Given strings str1 and str2. The task is to find that if str1 is substring in shuffled form of str2 or not. Print “YES” if str1 is substring in shuffled form of str2 else print “NO”.
Input: str1 = “onetwofour”, str2 = “hellofourtwooneworld”
Explanation: str1 is substring in shuffled form of str2 as
str2 = “hello” + “fourtwoone” + “world”
str2 = “hello” + str1 + “world”, where str1 = “fourtwoone” (shuffled form)
Hence str1 is a substring of str2 in shuffled form.
Input: str1 = “roseyellow”, str2 = “yellow”
Explanation: As length of str1 is greater than str2. Hence str1 is not a substring of str2.
Let n = length of str1, m = length of str2.
- If n > m, then string str1 can never be substring of str2.
- Else sort the string str1.
- Traverse string str2
- Put all the characters of str2 of length n in another string str.
- Sort the string str and Compare str and str1.
- If str = str1, then string str1 is a shuffled substring of string str2.
- else repeat the above process till ith index of str2 such that (i – n + 1 > m)(as after this index the length of remaining string str2 will be less than str1.
- If str is not equals to str1 in above steps, then string str1 can never be substring of str2.
Below is the implementation of the above approach:
Time Complexity: O(m*n*log(n)), where n = length of string str1 and m = length of string str2
Auxiliary Space: O(n)
Efficient Solution : This problem is a simpler version of Anagram Search. It can be solved in linear time using character frequency counting.
We can achieve O(n) time complexity under the assumption that alphabet size is fixed which is typically true as we have maximum 256 possible characters in ASCII. The idea is to use two count arrays:
1) The first count array store frequencies of characters in pattern.
2) The second count array stores frequencies of characters in current window of text.
The important thing to note is, time complexity to compare two count arrays is O(1) as the number of elements in them are fixed (independent of pattern and text sizes). Following are steps of this algorithm.
1) Store counts of frequencies of pattern in first count array countP. Also store counts of frequencies of characters in first window of text in array countTW.
2) Now run a loop from i = M to N-1. Do following in loop.
…..a) If the two count arrays are identical, we found an occurrence.
…..b) Increment count of current character of text in countTW
…..c) Decrement count of first character in previous window in countWT
3) The last window is not checked by above loop, so explicitly check it.
Following is the implementation of above algorithm.
- Queries to check if string B exists as substring in string A
- Check if a string is substring of another
- Python | Check if a Substring is Present in a Given String
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Check if string can be made lexicographically smaller by reversing any substring
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
- Find length of longest subsequence of one string which is substring of another string
- Minimum changes required to make first string substring of second string
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- String slicing in Python to check if a string can become empty by recursive deletion
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Check if a string can become empty by recursively deleting a given sub-string
- Check if a string can be obtained by rotating another string 2 places
- Check if any permutation of string is a K times repeated string
- Check if left and right shift of any string results into given string
- Check if a string can be obtained by rotating another string d places
- Check if a string contains a palindromic sub-string of even length
- Check whether second string can be formed from characters of first string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.