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 and only characters of A and B and order of all characters in individual strings is preserved.
Example:
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
Method 1: 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.
C++
bool isInterleaved(string A, string B, string C) {
if (A.empty() && B.empty() && C.empty()) {
return true ;
}
if (C.empty()) {
return false ;
}
return ((C[0] == A[0]) && isInterleaved(A.substr(1), B, C.substr(1)))
|| ((C[0] == B[0]) && isInterleaved(A, B.substr(1), C.substr(1)));
}
|
C
bool isInterleaved(
char * A, char * B, char * C)
{
if (!(*A || *B || *C))
return true ;
if (*C == '\0' )
return false ;
return ((*C == *A) && isInterleaved(
A + 1, B, C + 1))
|| ((*C == *B) && isInterleaved(
A, B + 1, C + 1));
}
|
Java
public static boolean isInterleaved(String A, String B, String C) {
if (A.isEmpty() && B.isEmpty() && C.isEmpty()) {
return true ;
}
if (C.isEmpty()) {
return false ;
}
return (C.charAt( 0 ) == A.charAt( 0 ) && isInterleaved(
A.substring( 1 ), B, C.substring( 1 )))
|| (C.charAt( 0 ) == B.charAt( 0 ) && isInterleaved(
A, B.substring( 1 ), C.substring( 1 )));
}
|
Python3
def isInterleaved(A, B, C):
if not A and not B and not C:
return True
if not C:
return False
if A and C[ 0 ] = = A[ 0 ]:
return isInterleaved(A[ 1 :], B, C[ 1 :])
if B and C[ 0 ] = = B[ 0 ]:
return isInterleaved(A, B[ 1 :], C[ 1 :])
return False
def test(A, B, C):
if (isInterleaved(A, B, C)):
print (C, "is interleaved of" , A, "and" , B)
else :
print (C, "is not interleaved of" , A, "and" , B)
if __name__ = = '__main__' :
test( "XXY" , "XXZ" , "XXZXXXY" )
test( "XY" , "WZ" , "WZXY" )
test( "XY" , "X" , "XXY" )
test( "YX" , "X" , "XXY" )
test( "XXY" , "XXZ" , "XXXXZY" )
|
C#
public static bool IsInterleaved( string A, string B, string C)
{
if ( string .IsNullOrEmpty(A) && string .IsNullOrEmpty(B) && string .IsNullOrEmpty(C))
{
return true ;
}
if ( string .IsNullOrEmpty(C))
{
return false ;
}
return (C[0] == A[0] && IsInterleaved(A.Substring(1), B, C.Substring(1)))
|| (C[0] == B[0] && IsInterleaved(A, B.Substring(1), C.Substring(1)));
}
|
Javascript
function isInterleaved(A, B, C) {
if (!A && !B && !C) {
return true ;
}
if (!C) {
return false ;
}
if (A && C[0] == A[0]) {
return isInterleaved(A.substring(1), B, C.substring(1));
}
if (B && C[0] == B[0]) {
return isInterleaved(A, B.substring(1), C.substring(1));
}
return false ;
}
function test(A, B, C) {
if (isInterleaved(A, B, C)) {
console.log(`${C} is interleaved of ${A} and ${B}`);
} else {
console.log(`${C} is not interleaved of ${A} and ${B}`);
}
}
test( "XXY" , "XXZ" , "XXZXXXY" );
test( "XY" , "WZ" , "WZXY" );
test( "XY" , "X" , "XXY" );
test( "YX" , "X" , "XXY" );
test( "XXY" , "XXZ" , "XXXXZY" );
|
Complexity Analysis:
- Time Complexity: O(2^n), where n is the length of the given string.
- Space Complexity: O(1).
The space complexity is constant.
Method 2: 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.
Algorithm:
- 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.
C++
#include <iostream>
#include <string.h>
using namespace std;
bool isInterleaved(
char * A, char * B, char * C)
{
int M = strlen (A), N = strlen (B);
bool IL[M + 1][N + 1];
memset (IL, 0, sizeof (IL));
if ((M + N) != strlen (C))
return false ;
for ( int i = 0; i <= M; ++i) {
for ( int j = 0; j <= N; ++j) {
if (i == 0 && j == 0)
IL[i][j] = true ;
else if (i == 0) {
if (B[j - 1] == C[j - 1])
IL[i][j] = IL[i][j - 1];
}
else if (j == 0) {
if (A[i - 1] == C[i - 1])
IL[i][j] = IL[i - 1][j];
}
else if (
A[i - 1] == C[i + j - 1]
&& B[j - 1] != C[i + j - 1])
IL[i][j] = IL[i - 1][j];
else if (
A[i - 1] != C[i + j - 1]
&& B[j - 1] == C[i + j - 1])
IL[i][j] = IL[i][j - 1];
else if (
A[i - 1] == C[i + j - 1]
&& B[j - 1] == C[i + j - 1])
IL[i][j]
= (IL[i - 1][j]
|| IL[i][j - 1]);
}
}
return IL[M][N];
}
void test( char * A, char * B, char * C)
{
if (isInterleaved(A, B, C))
cout << C << " is interleaved of "
<< A << " and " << B << endl;
else
cout << C << " is not interleaved of "
<< A << " and " << B << endl;
}
int main()
{
test( "XXY" , "XXZ" , "XXZXXXY" );
test( "XY" , "WZ" , "WZXY" );
test( "XY" , "X" , "XXY" );
test( "YX" , "X" , "XXY" );
test( "XXY" , "XXZ" , "XXXXZY" );
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG{
static boolean isInterleaved(String A, String B,
String C)
{
int M = A.length(), N = B.length();
boolean IL[][] = new boolean [M + 1 ][N + 1 ];
if ((M + N) != C.length())
return false ;
for ( int i = 0 ; i <= M; i++)
{
for ( int j = 0 ; j <= N; j++)
{
if (i == 0 && j == 0 )
IL[i][j] = true ;
else if (i == 0 )
{
if (B.charAt(j - 1 ) ==
C.charAt(j - 1 ))
IL[i][j] = IL[i][j - 1 ];
}
else if (j == 0 )
{
if (A.charAt(i - 1 ) ==
C.charAt(i - 1 ))
IL[i][j] = IL[i - 1 ][j];
}
else if (A.charAt(i - 1 ) ==
C.charAt(i + j - 1 ) &&
B.charAt(j - 1 ) !=
C.charAt(i + j - 1 ))
IL[i][j] = IL[i - 1 ][j];
else if (A.charAt(i - 1 ) !=
C.charAt(i + j - 1 ) &&
B.charAt(j - 1 ) ==
C.charAt(i + j - 1 ))
IL[i][j] = IL[i][j - 1 ];
else if (A.charAt(i - 1 ) ==
C.charAt(i + j - 1 ) &&
B.charAt(j - 1 ) ==
C.charAt(i + j - 1 ))
IL[i][j] = (IL[i - 1 ][j] ||
IL[i][j - 1 ]);
}
}
return IL[M][N];
}
static void test(String A, String B, String C)
{
if (isInterleaved(A, B, C))
System.out.println(C + " is interleaved of " +
A + " and " + B);
else
System.out.println(C + " is not interleaved of " +
A + " and " + B);
}
public static void main(String[] args)
{
test( "XXY" , "XXZ" , "XXZXXXY" );
test( "XY" , "WZ" , "WZXY" );
test( "XY" , "X" , "XXY" );
test( "YX" , "X" , "XXY" );
test( "XXY" , "XXZ" , "XXXXZY" );
}
}
|
Python3
def isInterleaved(A, B, C):
M = len (A)
N = len (B)
IL = [[ False ] * (N + 1 ) for i in range (M + 1 )]
if ((M + N) ! = len (C)):
return False
for i in range ( 0 , M + 1 ):
for j in range ( 0 , N + 1 ):
if (i = = 0 and j = = 0 ):
IL[i][j] = True
elif (i = = 0 ):
if (B[j - 1 ] = = C[j - 1 ]):
IL[i][j] = IL[i][j - 1 ]
elif (j = = 0 ):
if (A[i - 1 ] = = C[i - 1 ]):
IL[i][j] = IL[i - 1 ][j]
elif (A[i - 1 ] = = C[i + j - 1 ] and
B[j - 1 ] ! = C[i + j - 1 ]):
IL[i][j] = IL[i - 1 ][j]
elif (A[i - 1 ] ! = C[i + j - 1 ] and
B[j - 1 ] = = C[i + j - 1 ]):
IL[i][j] = IL[i][j - 1 ]
elif (A[i - 1 ] = = C[i + j - 1 ] and
B[j - 1 ] = = C[i + j - 1 ]):
IL[i][j] = (IL[i - 1 ][j] or IL[i][j - 1 ])
return IL[M][N]
def test(A, B, C):
if (isInterleaved(A, B, C)):
print (C, "is interleaved of" , A, "and" , B)
else :
print (C, "is not interleaved of" , A, "and" , B)
if __name__ = = '__main__' :
test( "XXY" , "XXZ" , "XXZXXXY" )
test( "XY" , "WZ" , "WZXY" )
test( "XY" , "X" , "XXY" )
test( "YX" , "X" , "XXY" )
test( "XXY" , "XXZ" , "XXXXZY" )
|
C#
using System;
class GFG
{
static bool isInterleaved( string A, string B,
string C)
{
int M = A.Length, N = B.Length;
bool [ , ] IL = new bool [M + 1, N + 1];
if ((M + N) != C.Length)
return false ;
for ( int i = 0; i <= M; i++)
{
for ( int j = 0; j <= N; j++)
{
if (i == 0 && j == 0)
IL[i, j] = true ;
else if (i == 0)
{
if (B[j - 1] == C[j - 1])
IL[i, j] = IL[i, j - 1];
}
else if (j == 0)
{
if (A[i - 1] == C[i - 1])
IL[i, j] = IL[i - 1, j];
}
else if (A[i - 1] == C[i + j - 1] &&
B[j - 1] != C[i + j - 1])
IL[i, j] = IL[i - 1, j];
else if (A[i - 1] != C[i + j - 1] &&
B[j - 1] == C[i + j - 1])
IL[i, j] = IL[i, j - 1];
else if (A[i - 1] == C[i + j - 1] &&
B[j - 1] == C[i + j - 1])
IL[i, j] = (IL[i - 1, j] ||
IL[i, j - 1]);
}
}
return IL[M, N];
}
static void test( string A, string B, string C)
{
if (isInterleaved(A, B, C))
Console.WriteLine(C + " is interleaved of " +
A + " and " + B);
else
Console.WriteLine(C + " is not interleaved of " +
A + " and " + B);
}
static void Main()
{
test( "XXY" , "XXZ" , "XXZXXXY" );
test( "XY" , "WZ" , "WZXY" );
test( "XY" , "X" , "XXY" );
test( "YX" , "X" , "XXY" );
test( "XXY" , "XXZ" , "XXXXZY" );
}
}
|
Javascript
<script>
function isInterleaved(A, B, C)
{
let M = A.length, N = B.length;
let IL = new Array(M + 1);
for (let i=0;i<M+1;i++){
IL[i] = new Array(N + 1).fill(0);
}
if ((M + N) != C.length)
return false ;
for (let i = 0; i <= M; ++i) {
for (let j = 0; j <= N; ++j) {
if (i == 0 && j == 0)
IL[i][j] = true ;
else if (i == 0) {
if (B[j - 1] == C[j - 1])
IL[i][j] = IL[i][j - 1];
}
else if (j == 0) {
if (A[i - 1] == C[i - 1])
IL[i][j] = IL[i - 1][j];
}
else if (
A[i - 1] == C[i + j - 1]
&& B[j - 1] != C[i + j - 1])
IL[i][j] = IL[i - 1][j];
else if (
A[i - 1] != C[i + j - 1]
&& B[j - 1] == C[i + j - 1])
IL[i][j] = IL[i][j - 1];
else if (
A[i - 1] == C[i + j - 1]
&& B[j - 1] == C[i + j - 1])
IL[i][j]
= (IL[i - 1][j]
|| IL[i][j - 1]);
}
}
return IL[M][N];
}
function test(A, B, C)
{
if (isInterleaved(A, B, C))
document.write(C + " is interleaved of " + A + " and " + B, "</br>" );
else
document.write(C + " is not interleaved of " + A + " and " + B, "</br>" );
}
test( "XXY" , "XXZ" , "XXZXXXY" );
test( "XY" , "WZ" , "WZXY" );
test( "XY" , "X" , "XXY" );
test( "YX" , "X" , "XXY" );
test( "XXY" , "XXZ" , "XXXXZY" );
</script>
|
Output:
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.
Complexity Analysis:
- Time Complexity: O(M*N).
Since a traversal of the 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.
https://youtu.be/WBXy-sztEwI
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Method 3: Dynamic Programming(Memoization)
Approach: We can make a matrix where rows and columns represent the characters of the string A and B. If C is the interleaved string of A and B then there exists a path from the top left of the Matrix to the bottom right. That is if we can go from index 0,0 to n,m while matching characters of all A and B with C then C is interleaved of A and B.
Let A be “XXY”, B be “XXZ” and C be “XXZXXY” then the path would look something like this:
let us consider one more example, let A be “ABC”, B be “DEF” and C be “ADBECF”, then the path would look something like this:
If there exists a path through which we can reach R, then C is the interleaved strings of A and B.
Algorithm:
1. We will first create a matrix dp to store the path since one path can be explored multiple times, the Matrix index dp[i][j] will store if there exists a path from this index or not.
2. If we are at i’th index of A and j’th index of B and C[i+j] matches both A[i] and B[j] then we explore both the paths that are we will go right and down i.e. we will explore index i+1,j and j+1,i.
3. If C[i+j] only matches with A[i] or B[j] then we will go just down or right respectively that is i+1,j or i,j+1.
C++
#include <iostream>
#include <string.h>
using namespace std;
int n, m;
int dp[101][101];
bool dfs( int i, int j, string &A, string &B, string &C);
bool isInterleave(string A, string B, string C)
{
n = A.length(), m = B.length();
if ((n + m) != C.length())
return 0;
for ( int i = 0; i <= n; i++)
for ( int j = 0; j <= m; j++)
dp[i][j] = -1;
return dfs(0, 0, A, B, C);
}
bool dfs( int i, int j, string &A, string &B, string &C)
{
if (dp[i][j] != -1)
return dp[i][j];
if (i == n && j == m)
return 1;
if (i < n && A[i] == C[i + j] && j < m && B[j] == C[i + j])
{
int x = dfs(i + 1, j, A, B, C), y = dfs(i, j + 1, A, B, C);
return dp[i][j] = x | y;
}
if (i < n && A[i] == C[i + j])
{
int x = dfs(i + 1, j, A, B, C);
return dp[i][j] = x;
}
if (j < m && B[j] == C[i + j])
{
int y = dfs(i, j + 1, A, B, C);
return dp[i][j] = y;
}
return dp[i][j] = 0;
}
void test(string A, string B, string C)
{
if (isInterleave(A, B, C))
cout << C << " is interleaved of "
<< A << " and " << B << endl;
else
cout << C << " is not interleaved of "
<< A << " and " << B << endl;
}
int main()
{
test( "XXY" , "XXZ" , "XXZXXXY" );
test( "XY" , "WZ" , "WZXY" );
test( "XY" , "X" , "XXY" );
test( "YX" , "X" , "XXY" );
test( "XXY" , "XXZ" , "XXXXZY" );
test( "ACA" , "DAS" , "DAACSA" );
return 0;
}
|
Java
import java.io.*;
class GFG {
static int n, m;
static int dp[][] = new int [ 100 ][ 100 ];
public static int dfs( int i, int j, String A, String B,
String C)
{
if (dp[i][j] != - 1 )
return dp[i][j];
if (i == n && j == m)
return 1 ;
if (i < n && A.charAt(i) == C.charAt(i + j) && j < m
&& B.charAt(j) == C.charAt(i + j)) {
int x = dfs(i + 1 , j, A, B, C),
y = dfs(i, j + 1 , A, B, C);
return dp[i][j] = x | y;
}
if (i < n && A.charAt(i) == C.charAt(i + j)) {
int x = dfs(i + 1 , j, A, B, C);
return dp[i][j] = x;
}
if (j < m && B.charAt(j) == C.charAt(i + j)) {
int y = dfs(i, j + 1 , A, B, C);
return dp[i][j] = y;
}
return dp[i][j] = 0 ;
}
public static int isInterleave(String A, String B,
String C)
{
n = A.length();
m = B.length();
if ((n + m) != C.length())
return 0 ;
for ( int i = 0 ; i <= n; i++)
for ( int j = 0 ; j <= m; j++)
dp[i][j] = - 1 ;
return dfs( 0 , 0 , A, B, C);
}
public static void test(String A, String B, String C)
{
if (isInterleave(A, B, C) != 0 )
System.out.println(C + " is interleaved of " + A
+ " and " + B);
else
System.out.println(C + " is not interleaved of "
+ A + " and " + B);
}
public static void main(String[] args)
{
test( "XXY" , "XXZ" , "XXZXXXY" );
test( "XY" , "WZ" , "WZXY" );
test( "XY" , "X" , "XXY" );
test( "YX" , "X" , "XXY" );
test( "XXY" , "XXZ" , "XXXXZY" );
test( "ACA" , "DAS" , "DAACSA" );
}
}
|
Python3
dp = [[ 0 ] * 101 ] * 101
def dfs(i, j, A, B, C):
if (dp[i][j]! = - 1 ):
return dp[i][j]
n,m = len (A), len (B)
if (i = = n and j = = m):
return 1
if (i<n and A[i] = = C[i + j] and j<m and B[j] = = C[i + j]):
x = dfs(i + 1 , j, A, B, C)
y = dfs(i, j + 1 , A, B, C)
dp[i][j] = x|y
return dp[i][j]
if (i < n and A[i] = = C[i + j]):
x = dfs(i + 1 , j, A, B, C)
dp[i][j] = x
return dp[i][j]
if (j < m and B[j] = = C[i + j]):
y = dfs(i, j + 1 , A, B, C)
dp[i][j] = y
return dp[i][j]
dp[i][j] = 0
return dp[i][j]
def isInterleaved(A, B, C):
n = len (A)
m = len (B)
if ((n + m)! = len (C)):
return 0
for i in range (n + 1 ):
for j in range (m + 1 ):
dp[i][j] = - 1
return dfs( 0 , 0 ,A,B,C)
def test(A, B, C):
if (isInterleaved(A, B, C)):
print (C, "is interleaved of" , A, "and" , B)
else :
print (C, "is not interleaved of" , A, "and" , B)
if __name__ = = '__main__' :
test( "XXY" , "XXZ" , "XXZXXXY" )
test( "XY" , "WZ" , "WZXY" )
test( "XY" , "X" , "XXY" )
test( "YX" , "X" , "XXY" )
test( "XXY" , "XXZ" , "XXXXZY" )
test( "ACA" , "DAS" , "DAACSA" )
|
C#
using System;
using System.Linq;
class GFG {
static int n, m;
static int [, ] dp = new int [100, 100];
public static int dfs( int i, int j, string A, string B,
string C)
{
if (dp[i, j] != -1)
return dp[i, j];
if (i == n && j == m)
return 1;
if (i < n && A[i] == C[i + j] && j < m
&& B[j] == C[i + j]) {
int x = dfs(i + 1, j, A, B, C),
y = dfs(i, j + 1, A, B, C);
return dp[i, j] = x | y;
}
if (i < n && A[i] == C[i + j]) {
int x = dfs(i + 1, j, A, B, C);
return dp[i, j] = x;
}
if (j < m && B[j] == C[i + j]) {
int y = dfs(i, j + 1, A, B, C);
return dp[i, j] = y;
}
return dp[i, j] = 0;
}
public static int isInterleave( string A, string B,
string C)
{
n = A.Length;
m = B.Length;
if ((n + m) != C.Length)
return 0;
for ( int i = 0; i <= n; i++)
for ( int j = 0; j <= m; j++)
dp[i, j] = -1;
return dfs(0, 0, A, B, C);
}
public static void test( string A, string B, string C)
{
if (isInterleave(A, B, C)==1)
Console.WriteLine(C + " is interleaved of " + A
+ " and " + B);
else
Console.WriteLine(C + " is not interleaved of "
+ A + " and " + B);
}
public static void Main( string [] args)
{
test( "XXY" , "XXZ" , "XXZXXXY" );
test( "XY" , "WZ" , "WZXY" );
test( "XY" , "X" , "XXY" );
test( "YX" , "X" , "XXY" );
test( "XXY" , "XXZ" , "XXXXZY" );
test( "ACA" , "DAS" , "DAACSA" );
}
}
|
Javascript
let n, m;
let dp = [];
function isInterleave(A, B, C) {
n = A.length;
m = B.length;
if (n + m !== C.length) {
return false ;
}
for (let i = 0; i <= n; i++) {
dp[i] = [];
for (let j = 0; j <= m; j++) {
dp[i][j] = -1;
}
}
return dfs(0, 0, A, B, C);
}
function dfs(i, j, A, B, C) {
if (dp[i][j] !== -1) {
return dp[i][j];
}
if (i === n && j === m) {
return true ;
}
if (i < n && A[i] === C[i + j] && j < m && B[j] === C[i + j]) {
let x = dfs(i + 1, j, A, B, C);
let y = dfs(i, j + 1, A, B, C);
dp[i][j] = x || y;
return dp[i][j];
}
if (i < n && A[i] === C[i + j]) {
let x = dfs(i + 1, j, A, B, C);
dp[i][j] = x;
return dp[i][j];
}
if (j < m && B[j] === C[i + j]) {
let y = dfs(i, j + 1, A, B, C);
dp[i][j] = y;
return dp[i][j];
}
dp[i][j] = false ;
return dp[i][j];
}
function test(A, B, C) {
if (isInterleave(A, B, C)) {
console.log(C + " is interleaved of " + A + " and " + B);
} else {
console.log(C + " is not interleaved of " + A + " and " + B);
}
}
function main() {
test( "XXY" , "XXZ" , "XXZXXXY" );
test( "XY" , "WZ" , "WZXY" );
test( "XY" , "X" , "XXY" );
test( "YX" , "X" , "XXY" );
test( "XXY" , "XXZ" , "XXXXZY" );
test( "ACA" , "DAS" , "DAACSA" );
}
main();
|
OutputXXZXXXY 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
DAACSA is interleaved of ACA and DAS
Complexity Analysis:
Time Complexity: O(m*n).
This is the worst case of time complexity, if the given strings contain no common character matching with C then the time complexity will be O(n+m).
Space Complexity: O(m*n).
This is the space required to store the DP array.
The above method and implementation are contributed by Anirudh Singh.