When this C++ program got runtime error, it works in my local enviroment, any help?


  • 0
    M
    class Solution {
    

    public:
    vector<int> findSubstring(string s, vector<string>& words) {

    vector<int> res;
    if(s.length()==0 || words.size()==0)
    return res;

    unordered_map<string, int> map;
    
    for(int i=0;i<words.size();i++)
    {
        if(map.find(words[i])!=map.end())
        {
            map[words[i]] = map[words[i]]+1;
        }
        else
        {
            map[words[i]] = 1;
        }
    }
    for(int i=0;i<words[0].length();i++)
    {
        unordered_map<string,int> curMap;
        int count = 0;
        int left = i;
        for(int j=i;j<=(int)s.length()-words[0].length();j+=words[0].length())
        {
            string str = s.substr(j,words[0].length());
            
            if(map.find(str)!=map.end())
            {
                if(curMap.find(str)!=curMap.end())
                    curMap[str]=curMap[str]+1;
                else
                    curMap[str]=1;
                if(curMap[str]<=map[str])
                    count++;
                else
                {
                    while(curMap[str]>map[str])
                    {
                        string temp = s.substr(left,words[0].length());
                        if(curMap.find(temp)!=curMap.end())
                        {
                            curMap[temp]=curMap[temp]-1;
                            if(curMap[temp]<map[temp])
                                count--;
                        }
                        left += words[0].length();
                    }
                }
                if(count == words.size())
                {
                    res.push_back(left);
                    //if(left<)
                    string temp = s.substr(left,words[0].length());
                    if(curMap.find(temp)!=curMap.end())
                        curMap[temp]=curMap[temp]-1;
                    count--;
                    left += words[0].length();
                }
            }
            else
            {
                curMap.clear();
                count = 0;
                left = j+words[0].length();
            }
        }
    }
    return res;
    

    }
    };


Log in to reply
 

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