This is a Leetcode problem:
You are given a string, S, and a list of words, L that are all of the same lengths. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.
Here is my solution to the problem:
class Solution:
def __init__(self, S, L):
self.S = S
self.L = L
def findSubstring(self, S, L):
res = [] # result list
num = len(L) # length of the str list
ls = len(S)
if num == 0:
return []
str_len = len(L[0]) # length of each str
#creating the map: counting the occurrence of each string
map_str = dict((x,L.count(x)) for x in set(L))
i = 0
while i + num * str_len - 1 < ls:
map_str2 = {}
j = 0
while j < num:
subs = S[i + j * str_len:i + j * str_len + str_len ]
if not subs in map_str:
break
else:
map_str2[subs] = map_str2.get(subs, 0) + 1
if map_str2[subs]>map_str[subs]:
break
j = j + 1
if j == num:
res.append(i)
i = i + 1
return res
S = "barfoothefoobarman"
L = ["bar","foo"]
index = Solution(S, L)
print(index.findSubstring(S, L))
Here are some example inputs/outputs:
S = "barfoothefoobarman"
L = ["bar", "foo"]
>>> [0, 9]
-------------------------------------------------------------------
S = "lingmindraboofooowingdingbarrwingmonkeypoundcake"
L = ["fooo", "barr", "wing", "ding", "wing"]
>>> [13]
So I would like to know whether I could make this program shorter and more efficient.
Any help would be highly appreciated.