Permutation In String

You are given two strings named str1 and str2 of length N and M respectively. Find whether string str2 contains one of the permutations of string str1 as its substring. In other words, whether there exists any permutation of string str1 that is a substring of str2.

For Example :

Input: str1 = “ab” , str2 = “aoba”
Output: True
Explanation : Permutations of first-string str1 i.e. “ab” are [ “ab”, “ba” ].
The substrings of str2 i.e. “aoba” are [ “a”, “o”, “b”, “a”, “ao”, “ob”, “ba”, “aob”, “oba”, “aoba” ].
The string “ba” is present in the list of substrings of string str2.
Input Format:
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘T’ lines represent the ‘T’ test cases.

The first and only line of each test case contains two single space-separated strings str1 and str2.
Output format :
 For each test case, print either “True” or “False”. 
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 10^4
1 <= M <= 10^4

Strings ‘str1’ and ‘str2’ consists only of lower case letters.

Time limit: 1 second
CodingNinjas
author
2y
Permutation In String
  • Check for Base Case i.e if Length of str1 > Length of str2, return false.As substring length is always greater than or equal to permutation never small.
  • Generate all the permutatio...read more
CodingNinjas
author
2y
Permutation In String
  • Check for Base Case i.e if Length of str1 > Length of str2, return false.
  • The idea behind the approach is that one string is a permutation of another string iff both strings contai...read more
CodingNinjas
author
2y
Permutation In String

Check for Base Case i.e if Length of str1 > Length of str2, return false.

One string will be a permutation of another string only if both of them contain the same characters with t...read more

CodingNinjas
author
2y
Permutation In String
  • Check for Base Case i.e if Length of str1 > Length of str2, return false.
  • Make two vectors named vec1 and vec2, where vec1 contains frequencies of every letter in str1, and vec2 co...read more
CodingNinjas
author
2y
Permutation In String
  • By updating in approach 4, instead of comparing all the elements of the vectors i.e vec1 and vec2.
  • For every updated vec2 corresponding to every window of str2 considered, keep a t...read more
Add answer anonymously...
PeopleStrong Full Stack Developer 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
Get AmbitionBox app

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