Why this code is slow, and get TLE??


  • 0
    R

    Here is my code, and I am wondering why if i remove the comment, it can get passed!!
    It is very strange, anyone can point why this code can't get passed ? thanks!

    class Solution {
    public:
        vector<int> findSubstring(string S, vector<string> &L) {
            map<string, int> map_t;
            map<string, int> map_temp;
            for (auto const& item : L)
                map_t[item]++;
                
            vector<int> result;
            int len_word = L[0].size();
            int totle_len = L.size() * len_word;
            int s_len = S.size();
            
            for (int i=0; i + totle_len <= s_len; i++) {
                map_temp = map_t;
                int j = i;
                for (; j<i+totle_len; j+=len_word) {
                    if (map_temp.find(S.substr(j, len_word)) == map_temp.end() || map_temp[S.substr(j, len_word)] == 0)
                        break;
                    map_temp[S.substr(j, len_word)]--;
                }
                if (j == i+totle_len) {
                    result.push_back(i);
                }
                // if (i == 1000)               if removes the comments , then it get accepted
                //    return result;
            }
            return result;
        }
    };

Log in to reply
 

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