Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

41. Find All Anagrams in a String

MediumDay 13 of 60Sliding Window

Given two strings s and p, return the list of every starting index in s where a substring begins that is an anagram (a rearrangement) of p. Return the indices in increasing order.

Example 1
Input: s = "cbaebabacd", p = "abc"
Output: [0,6]
"cba" starts at 0 and "bac" starts at 6.
Example 2
Input: s = "abab", p = "ab"
Output: [0,1,2]
"ab", "ba" and "ab" start at 0, 1 and 2.
Constraints
  • 1 <= s.length, p.length <= 3 * 10^4
  • Both strings contain only lowercase English letters
Hints
Frequently asked at: Amazon, Meta, Microsoft, Paytm
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min