【c++】leetcode438 找到字符串中所有字母异位词
1.题目
438. 找到字符串中所有字母异位词 - 力扣(LeetCode)
2.code
class Solution {
public:vector<int> findAnagrams(string s2, string s1) {vector<int> res{};unordered_map<char, int> need, window;for (char c : s1) need[c]++;int left = 0, right = 0, valid = 0;while (right < s2.length()){if (need.count(s2[right])){window[s2[right]]++;if (window[s2[right]] == need[s2[right]]){valid++;}}right++;while (valid == need.size()){if (right - left == s1.size()){res.push_back(left);}if (need.count(s2[left])){if (window[s2[left]] == need[s2[left]]){valid--;}window[s2[left]]--;}left++;}}return res;}
};