#C13492. Find All Anagram Start Indices
Find All Anagram Start Indices
Find All Anagram Start Indices
You are given two strings s
and p
. Your task is to find all the start indices of p
's anagrams in s
.
An anagram of a string is a rearrangement of its letters. For example, the anagrams of abc
include abc
, acb
, bac
, bca
, cab
, and cba
.
You should output the starting indices (0-indexed) of the substrings in s
that are anagrams of p
. The answer should be in ascending order.
The algorithm should consider the following well-known approach:
$$ \text{Use a sliding window of length } |p| \text{ over } s. \text{ Compare frequency counts of the current window with that of } p.\] \]
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains the string
s
. - The second line contains the string
p
.
Both strings consist of lowercase English letters.
outputFormat
Print a single line to stdout containing the start indices of p
's anagrams in s
separated by a space. If there are no such indices, print an empty line.
cbaebabacd
abc
0 6