Simple and easy to understand solution; c++


  • 0
    P
    public:
        int anagramPattern(string text, string pattern)
        {
            vector <int> countT(26, 0);
            for ( int i=0; i<text.size(); i++)
            {
                countT[text[i]-97]++;
                countT[pattern[i]-97]--;
            }
            for (int i=0; i<countT.size(); i++)
            {
                if(countT[i]!=0)
                    return 0;
            }
            return 1;
        }
        vector<int> findAnagrams(string s, string p) 
        {
            vector <int> result;
            if (p.size()>s.size())
                return result;
            for(int i=0; i<s.size()-p.size()+1; i++)
            {
                string str="";
                str = s.substr(i, p.size());
                
                int temp = anagramPattern(str,p);
                if (temp)
                    result.push_back(i);
            }
            return result;
        }
    };

Log in to reply
 

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