Guest User

Untitled

a guest
Apr 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. from collections import defaultdict
  2.  
  3. class Solution:
  4. def findSubstring(self, s, words):
  5. """
  6. :type s: str
  7. :type words: List[str]
  8. :rtype: List[int]
  9. """
  10. if not s or not words:
  11. return []
  12. location = []
  13.  
  14. wordsCount = defaultdict(int)
  15. for w in words:
  16. wordsCount[w] += 1
  17.  
  18. wl = len(words[0])
  19. for i in range(wl):
  20. left, j, cnt = i, i, 0
  21. tempWordsCount = defaultdict(int)
  22. while j <= len(s) - wl:
  23. w = s[j:j+wl]
  24. if w in wordsCount:
  25. tempWordsCount[w] += 1
  26. if tempWordsCount[w] <= wordsCount[w]:
  27. cnt += 1
  28. else:
  29. while tempWordsCount[w] > wordsCount[w]:
  30. ww = s[left:left+wl]
  31. tempWordsCount[ww] -= 1
  32. if tempWordsCount[ww] < wordsCount[ww]:
  33. cnt -= 1
  34. left += wl
  35. if cnt == len(words):
  36. location += [left]
  37. tempWordsCount[s[left:left+wl]] -= 1
  38. cnt -= 1
  39. left += wl
  40. else:
  41. tempWordsCount.clear()
  42. cnt = 0
  43. left = j + wl
  44. j += wl
  45. return location
Add Comment
Please, Sign In to add comment