567. Permutation in String
Good problem. My code definitely has improvement room: s1m == s2m leads to O(26n).
1class Solution:
2 def checkInclusion(self, s1: str, s2: str) -> bool:
3 s1m = [0] * 26
4 s2m = [0] * 26
5 n = len(s1)
6
7 def idx(c):
8 return ord(c) - ord("a")
9
10 for c in s1:
11 s1m[idx(c)] += 1
12
13 # let's slide
14 l = 0
15
16 for r, c in enumerate(s2):
17 if s1m == s2m:
18 return True
19
20 s2m[idx(c)] += 1
21
22 while (r - l + 1 > n) or (s2m[idx(c)] > s1m[idx(c)]):
23 s2m[idx(s2[l])] -= 1
24 l += 1
25
26 return s1m == s2m # "adc" vs. "dcda"
#Neetcode150 #Sliding-Window #Python #Revisit