567 Permutation In String
567 Permutation In String
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:
1
2
3
4
5
**Input:** s1 = "ab", s2 = "eidbaooo"
**Output:** true
**Explanation:** s2 contains one permutation of s1 ("ba").
Example 2:
1
2
3
4
**Input:** s1 = "ab", s2 = "eidboaoo"
**Output:** false
Constraints:
1
2
1 <= s1.length, s2.length <= 104
s1 and s2 consist of lowercase English letters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
start = 0
end = len(s1)-1
arr_old = [0] * 26
arr_new = [0] * 26
for k in s1:
arr_old[ord(k)-97] += 1
for k in s2[start:end+1]:
arr_new[ord(k)-97] += 1
#print(arr_new)
while(end < len(s2)-1):
if ''.join(map(str,arr_old)) == ''.join(map(str,arr_new)):
return True
#print(''.join(map(str,arr_old)), ''.join(map(str,arr_new)))
arr_new[ord(s2[start]) - 97] -= 1
start += 1
end += 1
arr_new[ord(s2[end]) - 97] += 1
if ''.join(map(str,arr_old)) == ''.join(map(str,arr_new)):
return True
return False
This post is licensed under CC BY 4.0 by the author.