Permutation in string
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
def compare(dict1, dict2):
for key in dict1:
if key not in dict2 or dict1[key] != dict2[key]:
return False
return True
count1 = Counter(s1)
l = 0
r = len(s1) - 1
while r < len(s2):
tmp = s2[l : r + 1]
count2 = Counter(tmp)
if compare(count1, count2):
return True
l += 1
r += 1
return False
Permutation in String
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.
Example 1:
Input: s1 = "ab", s2 = "eidbaooo"
Output: true
Explanation: s2 contains one permutation of s1 ("ba").
Example 2:
Input: s1 = "ab", s2 = "eidboaoo" Output: false
Constraints:
1 <= s1.length, s2.length <= 104s1ands2consist of lowercase English letters.