leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0438.cpp (894B)
0 class Solution {
1 typedef unordered_map<char, int> umci;
2 bool um_eq(const umci &goal, const umci &crnt) {
3 for (auto [k, v] : goal) {
4 const auto it = crnt.find(k);
5 if (it == crnt.end()) return false;
6 if ((*it).second != v) return false;
7 }
8 return true;
9 }
11 public:
12 vector<int> findAnagrams(string s, string p) {
13 if (p.size() > s.size()) return {};
14 unordered_map<char, int> goal, crnt;
16 for (int i = 0; i < p.size(); i++) {
17 goal[p[i]]++;
18 crnt[s[i]]++;
19 }
21 vector<int> res;
22 for (int i = p.size(); i < s.size(); i++) {
23 if (um_eq(goal, crnt)) res.push_back(i - p.size());
24 crnt[s[i - p.size()]]--;
25 crnt[s[i]]++;
26 }
27 if (um_eq(goal, crnt)) res.push_back(s.size() - p.size());
28 return res;
29 }
30 };