Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2c85f8d

Browse filesBrowse files
authored
Merge pull request #107 from kerms/patch-1
[May 18, 2020] Add cpp solution2
2 parents f8f49aa + eda0944 commit 2c85f8d
Copy full SHA for 2c85f8d

File tree

Expand file treeCollapse file tree

1 file changed

+21
-1
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+21
-1
lines changed

‎May-LeetCoding-Challenge/17-Find-All-Anagrams-In-A-String/Find-All-Anagrams-In-A-String.cpp

Copy file name to clipboardExpand all lines: May-LeetCoding-Challenge/17-Find-All-Anagrams-In-A-String/Find-All-Anagrams-In-A-String.cpp
+21-1Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,24 @@ class Solution {
3030

3131
return ans;
3232
}
33-
};
33+
};
34+
35+
class Solution2 {
36+
public:
37+
vector<int> findAnagrams(string s, string p) {
38+
vector<int> res, m(256, 0);
39+
int left = 0, right, count = p.size(), n = s.size();
40+
41+
if (count > n) return res;
42+
for (char &c : p) ++m[c];
43+
for (right = 0 ; right < p.size()-1 ; ++right)
44+
count -= m[s[right]]-- >= 1;
45+
46+
while (right < n) {
47+
count -= m[s[right++]]-- >= 1;
48+
if (count == 0) res.push_back(left);
49+
count += m[s[left++]]++ >= 0;
50+
}
51+
return res;
52+
}
53+
};

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.