Coding Test/LeetCode
[LeetCode] 567. Permutation in String c++
owls
2022. 12. 27. 18:00
728x90
- 문제
Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise.
In other words, return true if one of s1's permutations is the substring of s2.
Input: s1 = "ab", s2 = "eidbaooo"
Output: true
Explanation: s2 contains one permutation of s1 ("ba").
- 문제 해결
class Solution {
public:
bool checkInclusion(string s1, string s2) {
if( s1.size() > s2.size())
return false;
vector<int> vec(26, 0);
for(const auto& it : s1){
vec[it-'a']++;
}
vector<int> vec2(26, 0);
int l = 0, r = 0;
while( r < s2.size()){
vec2[s2[r]-'a']++;
if( r - l + 1 == s1.size() && vec == vec2){
return true;
}
if( r - l + 1 < s1.size()){
r++;
}
else{
vec2[s2[l] - 'a']--;
l++;
r++;
}
}
return false;
}
};
728x90