Anagram Substring Search

Given two strings 'STR' and 'PTR', identify all starting indices of 'PTR' anagram substrings in 'STR'. Two strings are anagrams if one can be rearranged to form the other.

Input:

First line: Integer T (number of test cases).
Each test case consists of:
Line 1: Two integers N and M.
Line 2: String 'STR' with N characters.
Line 3: String 'PTR' with M characters.

Output:

For each test case, output the list of starting indices of 'PTR' anagram substrings in 'STR', each consecutive list on a new line.

Example:

Input:
1
9 4
BACDGABCD
ABCD
Output:
0 5
Explanation:

The substrings at indices 0-3 ('BACD') and 5-8 ('ABCD') in 'STR' are anagrams of 'PTR'.

Constraints:

  • 1 ≤ T ≤ 50
  • 1 ≤ N, M ≤ 10⁴
  • Strings consist of only uppercase English letters.

Note:

No need to print anything in the function; return the answer.
AnswerBot
4mo

Find starting indices of anagram substrings of PTR in STR.

  • Iterate through STR using a sliding window of size M to check for anagrams with PTR.

  • Use a hashmap to store the frequency of characters in PTR ...read more

Help your peers!
Select
Add answer anonymously...

Codalien Technologies Software Engineer interview questions & answers

A Software Engineer was asked Q. Describe how you would build an authentication system for a bank.
A Software Engineer was asked Q. Anagram Substring Search Given two strings 'STR' and 'PTR', identify all startin...read more
A Software Engineer was asked Q. Construct Complete Binary Tree Problem Statement Given an array or list 'ARR' co...read more

Popular interview questions of Software Engineer

A Software Engineer was asked Q1. Describe how you would build an authentication system for a bank.
A Software Engineer was asked Q2. Anagram Substring Search Given two strings 'STR' and 'PTR', identify all startin...read more
A Software Engineer was asked Q3. Construct Complete Binary Tree Problem Statement Given an array or list 'ARR' co...read more
Codalien Technologies Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits