Simple Java solution


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

Log in to reply
 

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