- Permutation in String Medium
651
34
Favorite
Share Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string's permutations is the substring of 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
思路:活动窗口
代码:python3
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
m=len(s1)
n=len(s2)
if m>n:return False
s1hash=[0]*123
s2hash=[0]*123
for x in s1:
s1hash[ord(x)]+=1
for i in range(0,n):
s2hash[ord(s2[i])]+=1
if i>=m:
s2hash[ord(s2[i-m])]-=1
if s2hash==s1hash:
return True
return False
复制代码
转载于:https://juejin.im/post/5d0c3bcee51d4510803ce3cb