#K73122. Find All Anagrams in a String
Find All Anagrams in a String
Find All Anagrams in a String
Given two strings, s and p, your task is to find all starting indices of p's anagrams in s. An anagram is a permutation of the characters of a string. For example, if s = "cbaebabacd" and p = "abc", the substrings starting at indices 0 ("cba") and 6 ("bac") are anagrams of p.
The idea is to use a sliding window with frequency counts. In mathematical terms, two strings u and v are anagrams if and only if $$\forall c\in \Sigma,\ count_u(c)=count_v(c)$$ where (\Sigma) is the alphabet.
Output the indices in increasing order. If no anagrams exist, output nothing.
inputFormat
The input consists of two lines:
1. The first line contains the string s.
2. The second line contains the string p.
outputFormat
Print the starting indices of all anagrams of p in s, separated by a space, in increasing order. If there is no valid substring, output nothing.## sample
cbaebabacd
abc
0 6