Simple Java, Beats 100 %


  • 0

    public int countSegments(String s) {

        int c=0;
        if(s.length()==0)
        return c;
        for(int i=0;i<s.length();i++){
            if(s.charAt(i)!=' '){
            c++;
            
            while(i+1<s.length()&& s.charAt(i+1)!=' '){
                i++;
            }
            
            }
        }
        return c;
    }

Log in to reply
 

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