Find All Anagrams Problem Statement

Given a string STR and a non-empty string PTR, identify all the starting indices of anagrams of PTR within STR.

Explanation:

An anagram of a string is another string that can be rearranged using exactly the same characters. For example, "SILENT" and "LISTEN" are anagrams, while "ABA" and "ABB" are not.

Input:

The first line contains an integer 'T', the number of test cases. Each test case includes: 
- Two integers 'N' and 'M': the lengths of strings 'STR' and 'PTR' respectively.
- A string 'STR' of length 'N'.
- A string 'PTR' of length 'M'.

Output:

For each test case, print the list of starting indices of all anagram substrings of 'PTR' within 'STR'. Each result should be on a new line. 

Example:

Consider STR = "BACDGABCDA" and PTR = "ABCD".

Output:

[0, 5, 6]

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 105
  • 1 ≤ M ≤ N
  • Time limit: 1 second

Note:

  • Both strings STR and PTR contain only uppercase English letters.
  • If there are no anagram substrings, return an empty list.
  • If multiple anagrams exist, indices should be in ascending order.
  • You are only required to implement the function and return the result; printing is managed elsewhere.
AnswerBot
2d

Given a string STR and a non-empty string PTR, find all starting indices of anagrams of PTR within STR.

  • Create a frequency map of characters in PTR.

  • Use sliding window technique to check anagrams in STR...read more

Help your peers!
Add answer anonymously...
Walmart Senior Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter