2つの文字列s1とs2が与えられた場合、s2にs1の順列が含まれているかどうかを判別する関数を記述します。言い換えると、最初の文字列の順列の1つは、2番目の文字列のサブ文字列です。
例1:入力:s1 = "ab" s2 = "eidbaooo"出力:True
説明:s2にはs1( "ba")の順列の1つが含まれています。
例2:入力:s1 = "ab" s2 = "eidboaoo"出力:誤りソース:LeetCode
リンク:https ://leetcode-cn.com/problems/permutation-in-string
この質問は、「438。文字列内のすべての文字失読症を検索する」の解決策と非常によく似ています。
方法1:スライディングウィンドウを使用して、スライディングウィンドウの文字をs1の文字列と比較します。それらが同じである場合、それは考慮されます。
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
if len(s1)>len(s2): return False
record=Counter(s1)
length=len(s1)
win={}
l=0
rst=False # 不同处
for r,c in enumerate(s2):
if c not in record.keys():
win.clear()
l=r+1
else:
win[c]=win.get(c,0)+1
if r-l+1==length:
if win==record:
rst=True # 不同处
win[s2[l]]-=1
l+=1
return rst
方法2:方法1の変形
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
if len(s1) > len(s2):
return False
need = {}
window = {}
for i in s1:
if i in need:
need[i] += 1
else:
need[i] = 1
print(need,len(need))
left = 0
right = 0
valid = 0
while right < len(s2):
c = s2[right]
right += 1
if c in need:
# 窗口开始,在不在need和window中
if c in window:
window[c] += 1
else:
window[c] = 1
# 判断窗口函数值是否相同,相同则标记加1
if window[c] == need[c]:
valid += 1
if right - left >= len(s1):
# 当窗口长度大于目标字符串长度时
# 若valid等于字典need的长度时,即所有字符都比对完成时,还回 true
if valid == len(need):
return True
# 移动左边界,把最左边的字符清除出window
d = s2[left]
left += 1
if d in need:
# 如果这时需要清除的字符d在字典need中,则需要处理标记值valid和窗口值
if window[d] == need[d]:
valid -= 1
window[d] -= 1
return False