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.
- Check if a string is substring of another
- 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
- Find length of longest subsequence of one string which is substring of another string
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Length of longest substring to be deleted to make a string equal to another string
- Check if a string can be formed from another string using given constraints
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Minimum length of substring whose rotation generates a palindromic substring
- Check if it is possible to convert one string into another with given constraints
- Check if a string can be converted to another by swapping of adjacent characters of given type
- Python | Check if a Substring is Present in a Given String
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
- Check if a string can be obtained by rotating another string 2 places
- Check if a string can be repeated to make another string
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if a string can be obtained by rotating another string d places
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Check if a string is a scrambled form of another 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 firstname.lastname@example.org. 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.