permuting a string

🏠

https://leetcode.com/problems/permutation-in-string/

My solution. Basically a variation of Rabin Karp.

 1 from functools import reduce
 2 from string import ascii_lowercase as asc
 3 
 4 class Solution:
 5     def checkInclusion(self, s1: str, s2: str) -> bool:
 6         if len(s1) > len(s2):
 7             return False
 8         if len(s1) == 0:
 9             return True
10         d = {L:O for O, L in enumerate(asc)}
11         s1h = reduce(lambda a, b: a+d[b], s1, 0)
12         s2h = reduce(lambda a, b: a+d[b], s2[:len(s1)], 0)
13         for i in range(len(s2)-len(s1)+1):
14             if s1h == s2h:
15                 if all(l1 in s2[i:i+len(s1)] for l1 in s1):
16                     return True
17             if i+len(s1) == len(s2):
18                 break
19             s2h -= d[s2[i]]
20             s2h += d[s2[i+(len(s1))]]
21         return False
22 
23 # Solution().checkInclusion("ab", "eidboaoo")