Short and neat solution but slow.


  • 0
    W
    class Solution {
    public:
        vector<int> findSubstring(string s, vector<string>& words) {
            int len(words[0].size()),i,j;
            vector<int> result;
            vector<string> temp;
            for(i=0;i<s.size()-len*words.size()+1;++i){
                temp.clear();
                temp = words;
                for(j=i;j<i+len*words.size();j+=len){
                    auto p = find(temp.begin(),temp.end(),s.substr(j,len));
                    if(p==temp.end())    break;
                    else    temp.erase(p);
                }
                if(!temp.size())
                    result.push_back(i);
            }
            return result;
        }
    };
    

    It pass almost all the cases. But when comming with case "ababababababababa..........." ["ab","ba","ab","ba"........], which is the last case in all 169 case, it will reach the time limit. It's a neat solution but slow..


Log in to reply
 

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