Permutation in String
Given two stringss1ands2, write a function to return true ifs2contains the permutation ofs1. In other words, one of the first string's permutations is thesubstringof the second string.
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
分析
模板,注意是作为Min在while里被比较, 比较len(s1) == e-s?
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
lss,ll,s,e,counter,mm = len(s1),len(s2),0,0,len(s1),collections.Counter(list(s1))
while e < ll:
if mm[s2[e]] > 0:
counter -= 1
mm[s2[e]] -= 1
e += 1
while counter == 0:
if lss == e - s:
return True
if mm[s2[s]] == 0:
counter += 1
mm[s2[s]] += 1
s += 1
return False
Last updated
Was this helpful?