2ms Java solution, beats 100% java submission


  • 0
    I
    public int countSegments(String s) {
            int segCount = 0;
            boolean valid = false;
            for (char c : s.toCharArray()) {
                if (c != ' ' && !valid) 
                        valid = true;
                else if (c == ' ' && valid) {
                        valid = false;
                        segCount++;
                    }
             }
            return valid ? segCount + 1 : segCount;
        }
    

Log in to reply
 

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