c++ using map


  • 1
    X
    vector<int> findSubstring(string s, vector<string>& words) {
            vector<int> ans;
            int sLen = s.length();
            int size = words.size();
            if(sLen == 0 || size == 0){
                return ans;
            }
            int wordLen = words[0].length();
            unordered_map<string, int> m;
            for_each(words.begin(), words.end(), [&m](string word){++m[word];});
            for(int i = 0; i <= sLen - size * wordLen; ++i){
                auto m1 = m;
                int j = 0;
                for( ; j < size * wordLen; j += wordLen){
                    string tmp = s.substr(i + j, wordLen);
                    if(m1.find(tmp) != m1.end() && m1[tmp] > 0){
                        --m1[tmp];
                    }else{
                        break;
                    }
                }
                if(j == size * wordLen){
                    ans.push_back(i);
                }
            }
            return ans;
        }
    

  • 0
    C

    What's the time complexity of this method?


Log in to reply
 

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