Skip to content

Commit 29641c8

Browse files
authored
Merge pull request AlgoStudyGroup#110 from kerms/patch-2
[May 18, 2020] Add cpp solution2
2 parents d2ce2e3 + 2eba7a2 commit 29641c8

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

May-LeetCoding-Challenge/18-Permutation-In-String/Permutation-In-String.cpp

+27-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,30 @@ class Solution {
3030

3131
return false;
3232
}
33-
};
33+
};
34+
35+
class Solution2 {
36+
public:
37+
bool checkInclusion(string s1, string s2) {
38+
vector<int> h1(26,0);
39+
int count = s1.size();
40+
int right = 0, left = 0;
41+
int l_idx;
42+
43+
for (char &c : s1) ++h1[c-'a'];
44+
45+
for (int right = 0; right < s2.size() ; ++right) {
46+
count -= h1[s2[right]-'a']-- > 0;
47+
if (count == 0) {
48+
l_idx = right - s1.size() + 1;
49+
for (; left < l_idx; ++left)
50+
count += h1[s2[left]-'a']++ >= 0;
51+
52+
if (count == 0)
53+
return true;
54+
}
55+
}
56+
57+
return false;
58+
}
59+
};

0 commit comments

Comments
 (0)