3. 无重复字符的最长子串
class Solution {
public:int lengthOfLongestSubstring(string str) {int ans = 0;for (int i = 0; i < str.size(); i++) {set<int> s;for (int j = i; j < str.size(); j++) {if (s.find(str[j]) != s.end()) break;s.insert(str[j]);}ans = max(ans, (int)s.size());}return ans;}
};
利用字符数量很小的条件,暴力即可
438. 找到字符串中所有字母异位词
class Solution {
public:int work(char x) {int t = x - 'a';return t;}vector<int> findAnagrams(string s, string p) {int n = s.size(), m = p.size();vector<int> cnt(26, 0);vector<int> a(26, 0);for (int i = 0; i < m; i++) {a[work(p[i])]++;}vector<int> ans;int l = 0;for (int r = 0; r < n; r++) {cnt[work(s[r])]++;if (r - l + 1 == m) {if (cnt == a) {ans.push_back(l);}cnt[work(s[l])]--;l++;}}return ans;}
};
固定长度的滑动窗口,模拟即可