Advertisement
Guest User

substring permutations

a guest
Sep 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. from collections import defaultdict
  2.  
  3.  
  4. class Solution:
  5.     def compare_dicts(self, d1, d2):
  6.         for key in d1.keys():
  7.             if d1.get(key) != d2.get(key)  :
  8.                 return False
  9.         return True
  10.  
  11.     def checkInclusion(self, s1: str, s2: str) -> bool:
  12.  
  13.         ls1 = len(s1);    ls2 = len(s2)
  14.         s1_dict = defaultdict(int); s2_dict = defaultdict(int)
  15.  
  16.         for a in s1:
  17.             s1_dict[a] += 1
  18.  
  19.         for a in s2[:ls1]:
  20.             s2_dict[a] += 1
  21.  
  22.  
  23.         if self.compare_dicts(s1_dict, s2_dict):
  24.             return True
  25.  
  26.  
  27.  
  28.         for i in range(1,ls2-ls1+1):
  29.             s2_dict[s2[ls1 - 1 + i ]] += 1
  30.             s2_dict[s2[i-1]]      -= 1
  31.             if self.compare_dicts(s1_dict, s2_dict):
  32.                 return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement