
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
std::vector<int> idxs;
std::string dstr = getHash(p);
for (int i = 0; i<s.length(); i++) {
std::string sub_str = s.substr(i, p.length());
auto hash_str = getHash(sub_str);
if (hash_str == dstr) {
idxs.push_back(i);
}
}
return idxs;
}
std::string getHash(std::string str) {
int count[26] = {0};
for (int i = 0; i<str.length(); i++) {
count[str[i]-'a']++;
}
std::ostringstream os;
for (int j = 0; j<26; j++) {
if (count[j] > 0)
os << char(j+'a') << count[j];
}
return os.str();
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31