3ms simple and clean Java solution


  • 0
    S
    public int countSegments(String s) {
    	int count = 0;
    	boolean isStr = false;
    	for (char c : s.toCharArray()) {
    		if (isStr && c==' ') isStr = false;
    		if (!isStr && c!=' ') {count++; isStr = true;}
    	}
    	return count;
    }
    

Log in to reply
 

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