recursion moving char to the end of the string

后端 未结 3 1334
我寻月下人不归
我寻月下人不归 2021-01-24 15:04

i need to get a string and rearrange it with recursion by getting char and by that char i have to move that char everywhere on the string to the end like \"Hello world!\" ,\'l\

相关标签:
3条回答
  • 2021-01-24 15:35

    If you use recursion, it will be pretty expensive call for the result you are expecting. Lot of movement of String or charArray elements, eitherway you do. I don't see its a wiser choice. I would do it this way, it will be of space complexity O(2n) & performance complexity O(n).

    public class Solve {
    public static void main(String[] args) {
        System.out.println(ChToLast("Hello world!", 'l'));
    }
    
    public static String ChToLast(String str, char ch) {
        char[] chars = str.toCharArray();
        char[] modChars = new char[chars.length];
        int i = 0;      
        for(char element : chars){
            if(ch != element){
                modChars[i++] = element;
            }
        }
        Arrays.fill(modChars, i, chars.length , ch);
        return new String(modChars);
    }   
    }
    
    0 讨论(0)
  • 2021-01-24 15:35

    If you use while loop and write a method to check if that string means perfect statement then that may work for you Here you would need some help of NLP concept to check everytime if arranged chars are making any statement or are grammatically correct. This will help

    0 讨论(0)
  • 2021-01-24 15:38

    Recursion is the practise of reusing your method inside itself. In this case, I will provide a solution to explain what happens:

    public static String chrToLast(String str, char ch) {
        //This if statement details the end condition
        if(str.length() < 1) {
            return "";
        }
    
        String newString = str.substring(1); //Create new string without first character
    
        if(str.indexOf(ch) == 0) { //This happens when your character is found
            return chrToLast(newString, ch) + ch;
        } else { //This happens with all other characters
            return str.charAt(0) + chrToLast(newString, ch);
        }
    }
    

    If you execute:

    chrToLast("Hello, World!", 'l')
    

    This will result in the desired result: Heo, Word!lll

    Process

    In general, this method works by checking which character is currently the first in the given string, and then deciding what to do. If the first character is the same as the one your looking for (l), it will then remove that character from the string and use chrToLast on that new string. But, it also adds the character it found to the end of the result by using + ch. It continues to do this until there are no more characters left, which is what the end condition is for.

    The end condition

    The end condition returns an empty string "" because that is what is called the base case of the algorithm. You can think of a recursive algorithm as something solving a problem by calling itself a number of times. By calling themselves, recursive algorithms move towards a base. In this particular case, it does that by subtracting one character off the string each time the method is executed. Once there are no characters left, it reaches the base case which is "", where the string is finally empty and no characters can be subtracted anymore. (Hence it returns nothing as it's final state)

    I hope this answers your question. It's important to understand this concept, as it is very powerful. Try to study the code and comment if something's not clear.

    Something that can also help is by executing this code in an IDE and using the debugger to walk through its execution. You can then see for yourself what the flow of the program is, and see the value of the variables in play.

    0 讨论(0)
提交回复
热议问题