Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. class Solution:
  2.  
  3. # @param S, a string
  4. # @param L, a list of string
  5. # @return a list of integer
  6.  
  7. def __init__(self, S, L):
  8. self.S = S
  9. self.L = L
  10. def findSubstring(self, S, L):
  11. res = [] # result list
  12. num = len(L) # length of the str list
  13. ls = len(S)
  14. if num == 0:
  15. return []
  16. str_len = len(L[0]) # length of each str
  17.  
  18. #creating the map: counting the occurrence of each string
  19.  
  20. map_str = dict((x,L.count(x)) for x in set(L))
  21. i = 0
  22. while i + num * str_len - 1 < ls:
  23. map_str2 = {}
  24. j = 0
  25. while j < num:
  26. subs = S[i + j * str_len:i + j * str_len + str_len ]
  27. if not subs in map_str:
  28. break
  29. else:
  30.  
  31. map_str2[subs] = map_str2.get(subs, 0) + 1
  32. if map_str2[subs]>map_str[subs]:
  33. break
  34. j = j + 1
  35. if j == num:
  36. res.append(i)
  37. i = i + 1
  38.  
  39. return res
  40.  
  41. S = "barfoothefoobarman"
  42. L = ["bar","foo"]
  43.  
  44. index = Solution(S, L)
  45. print(index.findSubstring(S, L))
  46.  
  47. S = "barfoobarthefoobarman"
  48. L = ["bar","foo"]
  49. output = [0, 9]
  50.  
  51. S = "lingmindraboofooowingdingbarrwingmonkeypoundcake"
  52. L = ["fooo","barr","wing","ding","wing"]
  53. output = [13]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement