6 - lines C++ code using istringstream. Easy and clean.


  • 0
        int countSegments(string s) {
            istringstream in(s);
            string str = "";
            int cnt = 0;
            while(in >>str)
                cnt ++;
            return cnt;
        }
    

Log in to reply
 

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