1 static int wing=[]() 2 { 3 std::ios::sync_with_stdio(false); 4 cin.tie(NULL); 5 return 0; 6 }(); 7 8 class Solution 9 { 10 public: 11 vector<int> findAnagrams(string s, string p) 12 { 13 vector<int>res; 14 int lens=s.length(); 15 int lenp=p.length(); 16 if(lens<lenp) 17 return res; 18 vector<int> sv(26,0); 19 vector<int> pv(26,0); 20 for(char c:p) 21 pv[c-'a']++; 22 int max=lens-lenp+1; 23 for(int i=0;i<lenp;i++) 24 sv[s[i]-'a']++; 25 if(sv==pv) 26 res.push_back(0); 27 int i=0,j=lenp-1; 28 while(i<max-1) 29 { 30 sv[s[i++]-'a']--; 31 sv[s[++j]-'a']++; 32 if(sv==pv) 33 res.push_back(i); 34 } 35 return res; 36 } 37 };
用滑动窗口,移动判定