2ms Java Solution


  • 0
    A
    public int countSegments(String s) {
            if(s == null || s.length() == 0) return 0;
            s += " ";
            int count = 0;
            for(int i = 0; i < s.length(); i++){
                if(s.charAt(i) == ' ') continue;
                if(s.charAt(i) != ' ' && s.charAt(i + 1) == ' ') 
                    count++;
            }
            return count;
        }
    

Log in to reply
 

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