classSolution { public: intlengthOfLongestSubstring(string s){ int n = s.length(); int l = 0; int ans = 0; unordered_set<char> chSet; for (int r = 0; r < n; r ++) { while (chSet.count(s[r])) { chSet.erase(s[l]); l ++; } chSet.insert(s[r]); ans = max(ans, r-l+1); } return ans; } };
classSolution { public: vector<int> findAnagrams(string s, string p){ int sLen = s.length(); int pLen = p.length();
vector<int> pCount(26, 0); for (int i = 0; i < pLen; i ++) { pCount[p[i]-'a'] ++; } vector<int> ans; int l = 0; vector<int> sCount(26, 0); for (int r = 0; r < sLen; r ++) { sCount[s[r]-'a'] ++;
if (r - l + 1 > pLen) { sCount[s[l]-'a'] --; l ++; }