#C7901. Find All Anagrams in a String
Find All Anagrams in a String
Find All Anagrams in a String
You are given two strings, s
and p
. Your task is to find all the start indices in s
where an anagram of p
occurs. An anagram is a permutation of letters. For example, if s = "cbaebabacd"
and p = "abc"
, then the output should be [0, 6]
because the substrings starting at index 0 ("cba") and index 6 ("bac") are anagrams of p
.
Formally, if we denote by \( n = |s| \) and \( m = |p| \), you need to find all indices \( i \) (0-based indexing) such that the substring \( s[i \dots i+m-1] \) is a permutation of p
.
Note: The input strings consist of lowercase English letters.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains the string
s
. - The second line contains the string
p
.
outputFormat
Print a single line to standard output (stdout) containing the starting indices of all anagrams of p
in s
, separated by a space. If there are no such indices, print an empty line.
cbaebabacd
abc
0 6