Permutation in String Problem Statement

Determine if the string str2 contains a permutation of the string str1 as one of its substrings.

Input:

The first line contains an integer 'T', representing the number of test cases. For each test case, the first line contains two space-separated integers, 'N' and 'M', indicating the lengths of 'str1' and 'str2'. The second line contains the strings 'str1' and 'str2'.

Output:

For each test case, print "Yes" if any permutation of "str1" is a substring of "str2". Otherwise, print "No".

Example:

Input:
str1 = "ab", str2 = "aoba"
Output:
Yes
Explanation:

The permutations of "ab" are ["ab", "ba"]. The substring "ba" is found in "aoba".

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 5000
  • 1 <= M <= 5000
  • Time limit: 1 second

Note:

No printing of the result is needed; implement the function to return the answer.
AnswerBot
4mo

Check if a permutation of one string is a substring of another string.

  • Create a frequency map of characters in str1.

  • Iterate through str2 with a window of size N and check if the frequency map matches.

  • R...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at Amazon

Q. Could you describe the process for designing a data structure that allows for al...read more
Q. What is Java?
Q. Given two strings s and t, return true if they are equal when both are typed int...read more
Software Developer 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