Clean C++ solution


  • 0
    V
    class Solution {
    public:
        int countSegments(string s) {
            int numSegments = 0;
            if (s.length() == 0) {
                return 0;
            }
            for (int i=0; i<s.length(); i++) {
                if (i > 0 && (s[i] == ' ') && isValidChar(s[i-1])) {
                    numSegments += 1;
                }
            }
            return (isValidChar(s[s.length()-1]) ? numSegments + 1 : numSegments);
        }
        
        bool isValidChar(char c) {
            if (isblank(c))
                return false;
            return true;
        }
    };
    

Log in to reply
 

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