434.Number of Segments in a String
Example:
Input: "Hello, my name is John"
Output: 5class Solution {
public:
int countSegments(string s) {
int res=0;
bool ischar=false;
if(s.empty()) return res;
if(s.size()==1) return s[0]!=' ';
for(int i=1;i<s.length();i++)
if((s[i]==' ') && (s[i-1] !=' '))
{
res++;
//cout<<" "<< i<< res<<" "<<s[i]<<" "<<s[i-1]<<endl;
ischar=false;
}
else if(s[i]!=' ')
ischar=true;
return res+ischar;
}
};Last updated