diff --git a/test009.cpp b/test009.cpp new file mode 100644 index 0000000..81cbdcf --- /dev/null +++ b/test009.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + bool checkCnt(vector& cnts, vector& cntp){ + for(int i = 0; i < 26; i++){ + if(cnts[i] != cntp[i]) return false; + } + return true; + } + vector findAnagrams(string s, string p) { + vector cntp(26); + for(char c:p){ cntp[c-'a']++; } + + vector ret; + vector cnts(26); + for(int i = 0; i < min(p.length(), s.length()); i++){ + cnts[s[i] - 'a']++; + } + + if(checkCnt(cnts, cntp)) { ret.push_back(0);} + + for(int r = p.length(); r < s.length(); r++){ + int l = r - p.length() + 1; + cnts[s[l-1] - 'a']--; + cnts[s[r] - 'a']++; + if(checkCnt(cnts, cntp)) { ret.push_back(l); } + } + return ret; + } +}; \ No newline at end of file