Back to DSA sheet

Find All Anagrams in a String

MediumArrays

Given strings s and p, return the start indices of every substring of s that is an anagram of p (same letters, any order).

Input format (stdin): the first line is s, the second line is p. Output the start indices in ascending order, space separated (blank line if none).

Examples
Input: cbaebabacd abc
Output: 0 6
'cba' at 0 and 'bac' at 6 are anagrams of 'abc'.
Input: abab ab
Output: 0 1 2
Constraints
  • 1 <= |s|, |p| <= 3*10^4
  • lowercase English letters
Sheets
NeetCode 250
find-all-anagrams-in-a-string.cpp3 sample tests
Loading editor
Test results

Run the sample tests to check your solution against expected output.

Custom input (stdin)
Output

Run your code to see its output.