Given two strings S1 and S2 of equal length, the task is to determine if S2 is a scrambled form of S1.
Given string str, we can represent it as a binary tree by partitioning it to two non-empty substrings recursively.
Note: Srambled string is not same as an Anagram
Below is one possible representation of str = “coder”:
coder / \ co der / \ / \ c o d er / \ e r
To scramble the string, we may choose any non-leaf node and swap its two children.
Suppose, we choose the node “co” and swap its two children, it produces a scrambled string “ocder”.
ocder / \ oc der / \ / \ o c d er / \ e r
Thus, “ocder” is a scrambled string of “coder”.
Similarly, if we continue to swap the children of nodes “der” and “er”, it produces a scrambled string “ocred”.
ocred / \ oc red / \ / \ o c re d / \ r e
Thus, “ocred” is a scrambled string of “coder”.
Input: S1=”coder”, S2=”ocder”
“ocder” is a scrambled form of “coder”
Input: S1=”abcde”, S2=”caebd”
“caebd” is not a scrambled form of “abcde”
In order to solve this problem, we are using Divide and Conquer approach.
Given two strings of equal length (say n+1), S1[0…n] and S2[0…n]. If S2 is a scrambled form of S1, then there must exist an index i such that at least one of the following conditions is true:
- S2[0…i] is a scrambled string of S1[0…i] and S2[i+1…n] is a scrambled string of S1[i+1…n].
- S2[0…i] is a scrambled string of S1[n-i…n] and S2[i+1…n] is a scrambled string of S1[0…n-i-1].
Note: An optimization step to consider here is to check beforehand if the two strings are anagrams of each other. If not, it indicates that the strings contain different characters and can’t be a scrambled form of each other.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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 the given string is shuffled substring of another string
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Check if a string is suffix of another
- Check if it is possible to convert one string into another with given constraints
- Check if it is possible to transform one string to another
- Check if a string is substring of another
- Check if permutaion of one string can break permutation of another
- Check if one string can be converted to another
- Check if a string can be converted to another by swapping of adjacent characters of given type
- Check if a string can be transformed to another by sorting substrings
- Find the smallest window in a string containing all characters of another string
- Sort a string according to the order defined by another string
- Find length of longest subsequence of one string which is substring of another string
- Find if a string starts and ends with another given string
- Number of sub-strings which are anagram of any sub-string of another string
- Count occurrences of a string that can be constructed from another given 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.