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

Sign up
Appearance settings

Commit 797b6eb

Browse files
567. Permutation in String
1 parent 0480b2d commit 797b6eb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

‎permutation-in-string.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Runtime: 16 ms
2+
// Memory Usage: 7.3 MB
3+
class Solution {
4+
public:
5+
bool checkInclusion(string s1, string s2) {
6+
vector<int> h1(26, 0);
7+
vector<int> h2(26, 0);
8+
if (s2.size() < s1.size()) {
9+
return false;
10+
}
11+
for (char a : s1) {
12+
h1[a - 'a']++;
13+
}
14+
for (int i = 0; i < s1.size(); i++) {
15+
h2[s2[i] - 'a']++;
16+
}
17+
if (h1 == h2) {
18+
return true;
19+
}
20+
for (int i = s1.size(); i < s2.size(); i++) {
21+
h2[s2[i - s1.size()] - 'a']--;
22+
h2[s2[i] - 'a']++;
23+
if (h1 == h2) {
24+
return true;
25+
}
26+
}
27+
return false;
28+
}
29+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /