I'm working on improving my Python skills and have been doing a daily code problem. For the problem:
Given a word W and a string S, find all starting indices in S which are anagrams of W
For example s = "abxaba" and w = "ab" should return [0, 3, 4], s = "cbaebabacd", w = "abc" should return [0, 6]
Is there more efficient/pythonic solution than the code I wrote? Storing the queue as a list and casting it to set twice per loop feels like it must be inefficient.
def anagram_indices(w, s):
w = set(list(w))
queue = []
index = []
for x, i in enumerate(list(s)):
queue.append(i)
while(w.issubset(set(queue[1:]))):
queue.pop(0)
if w == set(queue):
index.append(x-len(w)+1)
return(index)
word = "ab"
string = "abxaba"
print(anagram_indices(word, string))
print(anagram_indices("abab", "abxba"))
returns[0, 3]
\$\endgroup\$