#K81742. Find All Anagrams in a String
Find All Anagrams in a String
Find All Anagrams in a String
Given two strings s
and p
, where s
is a non-empty string and p
is a non-empty pattern, find all the start indices in s
where p
's anagrams appear. All strings consist of lowercase English letters only. Your task is to output the indices in ascending order separated by a single space. If there is no anagram present, print an empty line.
To achieve an optimal solution, you might consider using a sliding window approach with a frequency count, which runs in \(O(n)\) time, where \(n\) is the length of string s
.
inputFormat
The input is read from the standard input (stdin) and consists of two lines:
- The first line contains the string
s
. - The second line contains the string
p
.
outputFormat
Output the starting indices of all anagrams of p
in s
in ascending order, with each index separated by a single space. If there are no valid starting indices, output an empty line.
cbaebabacd
abc
0 6