LC 434. Number of Segments in a String

Nilanjan Deb · April 5, 2020

This is my CPP solution.

class Solution {
public:
    int countSegments(string s) {
        int cnt = 0;
        vector<string> store;
        string container;
        for(int i=0;i<s.size();i++){
            if(s[i]==' '){
                if(container.size()!=0)
                    store.push_back(container);
                container.clear();
            }
            else
                container.push_back(s[i]);       
        }
        if(container.size()!=0)
            store.push_back(container);
        return store.size();
    }
};


Dicussion Forum