How advantage could it be by using split?

  • 0

    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;
        StringBuilder ans = new StringBuilder();
        int end=s.length();
        for(int i=s.length()-1;i>=0;i--){
            if(s.charAt(i)==' '){
                if(i>0) ans.append(' ');
                while(i-1>0&&s.charAt(i-1)==s.charAt(i)) 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.