Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Maintain a sliding window of constant length s1. Keep adding from the right and removing from left from the hashmap counter
- and compare the dictionary. If equal return True.
- """
- class Solution:
- def checkInclusion(self, s1: str, s2: str) -> bool:
- counter_s1 = {}
- window_s2 = {}
- for c in s1:
- counter_s1[c] = counter_s1.get(c, 0) + 1
- left = 0
- for c in s2[:len(s1)-1]:
- window_s2[c] = window_s2.get(c, 0) + 1
- for right in range(len(s1)-1, len(s2)):
- window_s2[s2[right]] = window_s2.get(s2[right], 0) + 1
- if counter_s1 == window_s2:
- return True
- else:
- window_s2[s2[left]] = window_s2.get(s2[left], 0) - 1
- if window_s2[s2[left]] == 0:
- window_s2.pop(s2[left])
- left = left + 1
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement