Created
February 27, 2022 02:07
-
-
Save TheSithPadawan/4c61585e7b7647287c875654d5a794ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
cnt = len(worddict) | |
while j < len(s): | |
cur = s[j:j+wordlen] | |
if cur not in worddict: | |
j += 1 | |
continue | |
if cur in worddict: | |
worddict[cur] -= 1 | |
if worddict[cur] == 0: | |
cnt -= 1 | |
while cnt == 0 and i < j: | |
if j + wordlen - i == total: | |
result_indices.append(i) | |
w = s[i: i+wordlen] | |
if w not in worddict: | |
i += 1 | |
continue | |
worddict[w] += 1 | |
if worddict[w] > 0: | |
cnt += 1 | |
i += wordlen | |
j += wordlen | |
return result_indices |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment