Java:
class Solution { public int countSegments(String s) { int res = 0; for(int i=0; i<s.length(); i++){ if((s.charAt(i)!=' ')&&(i==0||s.charAt(i-1)==' ')) res++; } return res; } }
Python:
class Solution: def countSegments(self, s): """ :type s: str :rtype: int """ return len(s.split())