Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<int> findAnagrams(string s, string p) {
- vector<int> result;
- if (s.length() < p.length()) {
- return result;
- }
- vector<int> freqP(26);
- for (char c : p) {
- freqP[c - 'a']++;
- }
- vector<int> freqS(26);
- for (int i = 0; i < p.length(); i++) {
- freqS[s[i] - 'a']++;
- }
- if (freqP == freqS) {
- result.push_back(0);
- }
- for (int i = 1; i <= s.length() - p.length(); i++) {
- freqS[s[i - 1] - 'a']--;
- freqS[s[i + p.length() - 1] - 'a']++;
- if (freqP == freqS) {
- result.push_back(i);
- }
- }
- return result;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment