letecode [438] - Find All Anagrams in a String

Posted lpomeloz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了letecode [438] - Find All Anagrams in a String相关的知识,希望对你有一定的参考价值。

Given a string s and a non-empty string p, find all the start indices of p‘s anagrams in s.

Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.

The order of output does not matter.

Example 1:

Input:
s: "cbaebabacd" p: "abc"

Output:
[0, 6]

Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".

Example 2:

Input:
s: "abab" p: "ab"

Output:
[0, 1, 2]

Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".

题目大意

  给定字符串s和非空字符串p,求在s中p的所有字母异位词子串,返回子串索引。

理  解:

  我自己的做法是:每遍历s的一个字符作为起始字符与p比较,结果超时了。时间复杂度O(m*n)。

  看别人滑动窗口的做法感觉非常优秀。用unordered_map保存字符串p

  初始滑动窗口left=right=0,判断right是否在p中出现,满足则右移right。否则左移left;若滑动窗口的字符串即为p的字母异位词,则记录left;更新left = left + 1,match = match - 1,以及Windows表。

  注意:移除当前出现次数未0的字符。

代 码 C++:

class Solution 
public:
    vector<int> findAnagrams(string s, string p) 
        vector<int> res;
        if(s=="" || s.length()<p.length())
            return res;
        unordered_map<char,int> need,window;
        unordered_map<char,int>::iterator itr,itrW;
        int left=0,right=0,match = 0,j;
            
        // p字符串存在need哈希表中
        for(int i=0;i<p.size();++i)
            itr = need.find(p[i]);
            if(itr!=need.end())
                itr->second++;
            else
               need.insert(pair<char,int>(p[i],1));
        
        while(left<=right && right<s.length())
            itr = need.find(s[right]);
            if(itr!=need.end())        // s[right] 出现在need中
                itrW = window.find(s[right]);
                if(itrW!=window.end())         // s[right] 出现在window中
                    itrW->second++;
                    if(itrW->second > itr->second)         // s[right]数量过多,从left往后的第一个s[right]重新开始
                        j = s.find(s[right],left);
                        while(left<=j)
                            if((window.find(s[left]))->second-- == (need.find(s[left]))->second)
                                if((window.find(s[left]))->second==0)
                                    window.erase(s[left]);
                                match--;
                            
                            left++;
                        
                    else if(itrW->second == itr->second)      // s[right]数量满足条件
                        match++;
                    
                else      // s[right] 插入window中
                    window.insert(pair<char,int>(s[right],1));
                    if(itr->second == 1)
                        match++;
                
                right++; 
            else          // s[right] 未出现在need中,重新开始构建窗口
                window.clear();
                left = right + 1;
                right = left;
                match = 0;
            
            if(match==need.size())     // 找到符合条件的子串,窗口左指针左移一位
                res.push_back(left);
                match--;
                if ((window.find(s[left]))->second == 1)
                    window.erase(s[left]);
                else
                    (window.find(s[left]))->second--;
                left++;
            
        
        return res;
    
;

运行结果:

  执行用时 :68 ms, 在所有 C++ 提交中击败了48.43%的用户

  内存消耗 :12.8 MB, 在所有 C++ 提交中击败了15.12%的用户

以上是关于letecode [438] - Find All Anagrams in a String的主要内容,如果未能解决你的问题,请参考以下文章

438. Find All Anagrams in a String

438. Find All Anagrams in a String 438.查找字符串中的所有Anagrams

438. Find All Anagrams in a String 438.查找字符串中的所有Anagrams

Leetcode 438. Find All Anagrams in s String

[leetcode-438-Find All Anagrams in a String]

438. Find All Anagrams in a String