A Java solution


  • 0
    X

    public class Solution {
    public int countSegments(String s) {
    s = s.trim();
    if(s == null || s.length() == 0) return 0;
    int res = 0;
    for(int i = 0; i < s.length(); i++) {
    if(s.charAt(i) == ' ' && (i == 0 || s.charAt(i - 1) != ' ')) res++;
    }
    return res+1;
    }
    }


Log in to reply
 

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