Given three strings A, B and C. Write a function that checks whether C is an interleaving of A and B. C is said to be interleaving A and B, if it contains all characters of A and B and order of all characters in individual strings is preserved.
Input: strings: "XXXXZY", "XXY", "XXZ" Output: XXXXZY is interleaved of XXY and XXZ The string XXXXZY can be made by interleaving XXY and XXZ String: XXXXZY String 1: XX Y String 2: XX Z Input: strings: "XXY", "YX", "X" Output: XXY is not interleaved of YX and X XXY cannot be formed by interleaving YX and X. The strings that can be formed are YXX and XYX
Simple Solution: Recursion.
Approach: A simple solution is discussed here: Check whether a given string is an interleaving of two other given string.
The simple solution doesn’t work if the strings A and B have some common characters. For example, let the given string be A and the other strings be B and C. Let A = “XXY”, string B = “XXZ” and string C = “XXZXXXY”. Create a recursive function that takes parameters A, B, and C. To handle all cases, two possibilities need to be considered.
- If the first character of C matches the first character of A, we move one character ahead in A and C and recursively check.
- If the first character of C matches the first character of B, we move one character ahead in B and C and recursively check.
If any of the above function returns true or A, B and C are empty then return true else return false.
Thanks to Frederic for suggesting this approach.
- Time Complexity: O(2^n), where n is the length of the given string.
For every character of the given string, there can be 2 options so the time complexity is O(2^n).
- Space Complexity: O(1).
The space complexity is constant.
Efficient Solution: Dynamic Programming.
Approach: The above recursive solution certainly has many overlapping sub-problems. For example, if we consider A = “XXX”, B = “XXX” and C = “XXXXXX” and draw a recursion tree, there will be many overlapping subproblems. Therefore, like any other typical Dynamic Programming problems, we can solve it by creating a table and store results of sub-problems in a bottom-up manner. The top-down approach of the above solution can be modified by adding a Hash Map.
- Create a DP array (matrix) of size M*N, where m is the size of the first string and n is the size of the second string. Initialize the matrix to false.
- If the sum of sizes of smaller strings is not equal to the size of the larger string then return false and break the array as they cant be the interleaved to form the larger string.
- Run a nested loop the outer loop from 0 to m and the inner loop from 0 to n. Loop counters are i and j.
- If the values of i and j are both zeroes then mark dp[i][j] as true. If the value of i is zero and j is non zero and the j-1 character of B is equal to j-1 character of C the assign dp[i][j] as dp[i][j-1] and similarly if j is 0 then match i-1 th character of C and A and if it matches then assign dp[i][j] as dp[i-1][j].
- Take three characters x, y, z as (i-1)th character of A and (j-1)th character of B and (i + j – 1)th character of C.
- if x matches with z and y does not match with z then assign dp[i][j] as dp[i-1][j] similarly if x is not equal to z and y is equal to z then assign dp[i][j] as dp[i][j-1]
- if x is equal to y and y is equal to z then assign dp[i][j] as bitwise OR of dp[i][j-1] and dp[i-1][j].
- return value of dp[m][n].
Thanks to Abhinav Ramana for suggesting this method of implementation.
XXZXXXY is not interleaved of XXY and XXZ WZXY is interleaved of XY and WZ XXY is interleaved of XY and X XXY is not interleaved of YX and X XXXXZY is interleaved of XXY and XXZ
See this for more test cases.
- Time Complexity: O(M*N).
Since a traversal of DP array is needed, so the time complexity is O(M*N).
- Space Complexity: O(M*N).
This is the space required to store the DP array.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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 given string can be formed by two other strings or their permutations
- Given two numbers as strings, find if one is a power of other
- Find the longest string that can be made up of other strings from the array
- Check whether two strings are anagram of each other
- Check if two given strings are isomorphic to each other
- Check if two strings are permutation of each other
- Check whether two strings are anagrams of each other using unordered_map in C++
- Check if two strings can be made equal by swapping one character among each other
- Minimize count of given operations required to make two given strings permutations of each other
- C Program to check if two given strings are isomorphic to each other
- Strings from an array which are not prefix of any other string
- Meta Strings (Check if two strings can become same after a swap in one string)
- Replace two substrings (of a string) with each other
- A Program to check if strings are rotations of each other or not
- Sub-strings that start and end with one character and have at least one other
- Check if strings are rotations of each other or not | Set 2
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- Pairs of complete strings in two sets of strings
- Number of common base strings for two strings