Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- class Solution:
- def compare_dicts(self, d1, d2):
- for key in d1.keys():
- if d1.get(key) != d2.get(key) :
- return False
- return True
- def checkInclusion(self, s1: str, s2: str) -> bool:
- ls1 = len(s1); ls2 = len(s2)
- s1_dict = defaultdict(int); s2_dict = defaultdict(int)
- for a in s1:
- s1_dict[a] += 1
- for a in s2[:ls1]:
- s2_dict[a] += 1
- if self.compare_dicts(s1_dict, s2_dict):
- return True
- for i in range(1,ls2-ls1+1):
- s2_dict[s2[ls1 - 1 + i ]] += 1
- s2_dict[s2[i-1]] -= 1
- if self.compare_dicts(s1_dict, s2_dict):
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement