#C7400. Find Anagram Indices
Find Anagram Indices
Find Anagram Indices
You are given two strings s
and word
. Your task is to find all starting indices in s
where an anagram of word
appears as a contiguous substring.
An anagram is a rearrangement of the letters of a word so that the frequency of each character is the same. In this problem, you should use a sliding window approach to efficiently compare the frequency counts.
Let \( n = |s| \) and \( m = |word| \). For every substring of s
of length \( m \), if its character frequency matches that of word
, then the starting index of that substring is included in the output.
Note: The indices should be printed in ascending order, separated by a single space. If there is no valid index, print an empty line.
inputFormat
The input consists of two lines read from standard input:
- The first line contains the string
s
. - The second line contains the string
word
, which is non-empty.
outputFormat
Output a single line containing the starting indices of each substring in s
that is an anagram of word
. The indices should be separated by a single space. If no such substring exists, output an empty line.
cbaebabacd
abc
0 6