Skip to content

Instantly share code, notes, and snippets.

@TheSithPadawan
Created February 27, 2022 02:18
Show Gist options
  • Save TheSithPadawan/be62383867e82d7c509bbded0dc21871 to your computer and use it in GitHub Desktop.
Save TheSithPadawan/be62383867e82d7c509bbded0dc21871 to your computer and use it in GitHub Desktop.
# leetcode 30.
def find_word_concatenation(s, words):
result_indices = []
worddict = dict()
wordlen = len(words[0])
i, j = 0, 0
total = 0
for word in words:
if word not in worddict:
worddict[word] = 0
worddict[word] += 1
total += len(word)
for i in range(len(s) - len(words) * wordlen + 1):
seen = dict()
for j in range(len(words)):
start = i + j * wordlen
w = s[start : start + wordlen]
if w not in worddict:
break
if w not in seen:
seen[w] = 0
seen[w] += 1
if seen[w] > worddict[w]:
break
# add start index to result if we are done with all the words
if j + 1 == len(words):
result_indices.append(i)
return result_indices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment