-
[LeetCode] 567. Permutation in String c++Coding Test/LeetCode 2022. 12. 27. 18:00728x90
- 문제
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'Coding Test > LeetCode' 카테고리의 다른 글
[LeetCode] 733. Flood Fill c++ (0) 2022.12.28 [LeetCode] 1876. Substrings of Size Three with Distinct Characters c++ (0) 2022.12.27 [LeetCode] 3. Longest Substring Without Repeating Characters c++ (0) 2022.12.27 [LeetCode] 19. Remove Nth Node From End of List c++ (0) 2022.12.27 [LeetCode] 876. Middle of the Linked List c++ (0) 2022.12.26