Predict the output
What does this print?
from collections import Counter
text, pat = "cbaebabacd", "abc"
m = len(pat)
starts = [i for i in range(len(text) - m + 1)
if Counter(text[i:i + m]) == Counter(pat)]
print(starts)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A [0, 6]
Right: 'cba' at 0 and 'bac' at 6.
This brute version re-counts every window; the page's solution gets the same starts by updating counts as the window slides.
Why not the others?
- B [0]. 'bac' at index 6 has the same letters too.
- C [0, 5, 6]. 'aba' at index 5 has two a's and no c.