![]() |
VOOZH | about |
Given strings str1 and str2. The task is to find if str1 is a substring in the shuffled form of str2 or not. Print "YES" if str1 is a substring in shuffled form of str2 else print "NO".
Example
Input: str1 = "onetwofour", str2 = "hellofourtwooneworld"
Output: YES
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"
Output: NO
Explanation: As the length of str1 is greater than str2. Hence, str1 is not a substring of str2.
Approach:
Let n = length of str1, m = length of str2.
Below is the implementation of the above approach:
YES
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 of 256 possible characters in ASCII. The idea is to use two count arrays:
1) The first count array stores frequencies of characters in a pattern.
2) The second count array stores frequencies of characters in the current window of text.
The important thing to note is, time complexity to compare two counted arrays is O(1) as the number of elements in them is fixed (independent of pattern and text sizes). The 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 the 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 the first character in the previous window in countWT[]
3) The last window is not checked by the above loop, so explicitly check it.
The following is the implementation of the above algorithm.
Yes
Time Complexity: O(M + (N-M)*256) where M is size of input string pat and N is size of input string txt. This is because one for loop runs from 0 to M and contributes O(M) time. Also, another for loop runs from M to N in which compare function is executed which runs in O(256) time which consequently results in O((N-m)*256) time complexity. So overall time complexity becomes O(M + (N-M)*256).
Space Complexity: O(256) as countP and countTW arrays of size MAX i.e, 256 has been created.