Find All Anagrams in a String

Given a string original and a string check, find the starting index of all substrings of original that is an anagram of check. The output must be sorted in ascending order.


  • original: A string
  • check: A string


  • A list of integers representing the starting indices of all anagrams of check.


Example 1

Input: original = "cbaebabacd", check = "abc"

Output: [0, 6]

Explanation: The substring from 0 to 2, "cba", is an anagram of "abc", and so is the substring from 6 to 8, "bac".

Example 2

Input: original = "abab", check = "ab"

Output: [0, 1, 2]

Explanation: All substrings with length 2 from "abab" is an anagram of "ab".


  • 1 <= len(original), len(check) <= 10^5
  • Each string consists of only lowercase characters in standard English alphabet.

Try it yourself




Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Contrary to popular belief, Lorem Ipsum is not simply random text.

  >>> a = [1, 2, 3]
  >>> a[-1]

Get premium for instant access to all content and solutions