How advantage could it be by using split?


  • 0
    R

    I don't actually know the function split(), and I'm gonna read the document about it. It seems lot of the solutions shared here are using split(). But I do know the trim(), lol.

    And here is my low-b -_-!, straight forward algorithm,

    public class Solution {
    public String reverseWords(String s) {
        s=s.trim();
        s=' '+s;
        StringBuilder ans = new StringBuilder();
        int end=s.length();
        for(int i=s.length()-1;i>=0;i--){
            if(s.charAt(i)==' '){
                ans.append(s.substring(i+1,end));
                if(i>0) ans.append(' ');
                while(i-1>0&&s.charAt(i-1)==s.charAt(i)) i--;
                end=i;
            }
        }
        return ans.toString();
    }
    

    }

    While dealing with large inputs, how faster could it be by using split?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.