|
| 1 | +class Solution: |
| 2 | + #generate the sub string |
| 3 | + def perm(self, s:str)->str: |
| 4 | + if len(s) <= 1: |
| 5 | + return [s] |
| 6 | + |
| 7 | + s1 = [] |
| 8 | + for i in range(len(s)): |
| 9 | + for j in self.perm(s[0:i] + s[i + 1:]): |
| 10 | + s1.append(s[i] + j) |
| 11 | + |
| 12 | + return s1 |
| 13 | + |
| 14 | + def checkInclusion(self, s1:str, s2:str)->bool: |
| 15 | + if (len(s1) > len(s2)): |
| 16 | + return False |
| 17 | + |
| 18 | + s1_perm = list(set(self.perm(s1))) |
| 19 | + #check if the perm str of s1 is the substr of s2 |
| 20 | + for i in s1_perm: |
| 21 | + idx = s2.find(i) |
| 22 | + if idx != -1: |
| 23 | + return True |
| 24 | + |
| 25 | + return False |
| 26 | + |
| 27 | +class Solution: |
| 28 | + def match(self, s1:str, s2:str)->bool: |
| 29 | + for i in range(26): |
| 30 | + if s1[i] != s2[i]: |
| 31 | + return False |
| 32 | + return True |
| 33 | + |
| 34 | + def checkInclusion(self, s1:str, s2:str)->bool: |
| 35 | + if (len(s1) > len(s2)): |
| 36 | + return False |
| 37 | + |
| 38 | + list1 = [0 for i in range(26)] |
| 39 | + list2 = [0 for i in range(26)] |
| 40 | + |
| 41 | + for i in range(len(s1)): |
| 42 | + list1[ord(s1[i]) - ord['a']] += 1 |
| 43 | + list2[ord(s2[i]) - ord['a']] += 1 |
| 44 | + |
| 45 | + for i in range(len(s2) - len(s1)): |
| 46 | + if self.match(list1, list2): |
| 47 | + return True |
| 48 | + list2[ord(s2[i + len(s1)]) - ord('a')] += 1 |
| 49 | + list2[ord(s2[i]) - ord('a')] -= 1 |
| 50 | + |
| 51 | + return self.match(list1, list2) |
| 52 | + |
| 53 | +class Solution: |
| 54 | + def checkInclusion(self, s1:str, s2:str)->bool: |
| 55 | + if (len(s1)) > len(s2): |
| 56 | + return False |
| 57 | + list1 = [0 for in range(26)] |
| 58 | + list2 = [0 for in range(26)] |
| 59 | + |
| 60 | + for i in range(len(s1)): |
| 61 | + list1[ord(s1[i]) - ord('a')] += 1 |
| 62 | + list2[ord(s2[i]) - ord('a')] += 1 |
| 63 | + |
| 64 | + count = 0 |
| 65 | + for i in range(26): |
| 66 | + if list1[i] == list2[i]: |
| 67 | + count += 1 |
| 68 | + |
| 69 | + for i in range(len(s2) - len(s1)): |
| 70 | + r = ord(s2[i + len(s1)]) - ord('a') |
| 71 | + l = ord(s2[i]) - ord('a') |
| 72 | + if count == 26: |
| 73 | + return True |
| 74 | + |
| 75 | + list2[r] += 1 |
| 76 | + if list2[r] == list1[r]: |
| 77 | + count += 1 |
| 78 | + elif list2[r] == list1[r] + 1: |
| 79 | + count -= 1 |
| 80 | + |
| 81 | + list2[l] -= 1 |
| 82 | + if list2[l] == list1[1]: |
| 83 | + count += 1 |
| 84 | + elif list2[l] == list1[l] - 1: |
| 85 | + count -= 1 |
| 86 | + |
| 87 | + return count == 26 |
0 commit comments