给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引。
字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 20100。
说明:
字母异位词指字母相同,但排列不同的字符串。 不考虑答案输出的顺序。 示例 1:
输入: s: “cbaebabacd” p: “abc”
输出: [0, 6]
解释: 起始索引等于 0 的子串是 “cba”, 它是 “abc” 的字母异位词。 起始索引等于 6 的子串是 “bac”, 它是 “abc” 的字母异位词。 示例 2:
输入: s: “abab” p: “ab”
输出: [0, 1, 2]
解释: 起始索引等于 0 的子串是 “ab”, 它是 “ab” 的字母异位词。 起始索引等于 1 的子串是 “ba”, 它是 “ab” 的字母异位词。 起始索引等于 2 的子串是 “ab”, 它是 “ab” 的字母异位词。
滑动窗口:
class Solution { public: vector<int> findAnagrams(string s, string p) { vector<int> res; vector<int> freq_p(26,0); for(char c:p) freq_p[c - 'a'] += 1; vector<int> freq_s(26,0); int l = 0, r = -1; while(r + 1 < s.size()){ r++; freq_s[s[r] - 'a'] ++; if(r - l + 1 > p.size()) freq_s[s[l++] - 'a']--; if(r - l + 1 == p.size() && same(freq_s, freq_p)) res.push_back(l); } return res; } private: bool same(const vector<int>& freq_s, const vector<int>& freq_p){ for(int i = 0; i < 26; i++) if(freq_s[i] != freq_p[i]) return false; return true; } };