Geeksforgeeks

Error

×

Given strings **A**, **B**, and **C**, find whether **C** is formed by an interleaving of **A** and **B**.

An interleaving of two strings S and T is a configuration where they are divided into **non-empty** substrings such that:

- S = S
_{1}+ S_{2}+ ... + S_{N} - T = T
_{1}+ T_{2}+ ... + T_{M} - |N - M| <= 1
- The interleaving is S
_{1}+ T_{1}+ S_{2}+ T_{2}+ S_{3}+ T_{3}+ ... or T_{1}+ S_{1}+ T_{2}+ S_{2}+ T_{3}+ S_{3}...

Note: **a + b** is the concatenation of strings a and b.

Return **true** if **C** is formed by an interleaving of **A** and **B**, else return **false.**

**Example 1:**

**Input:
**A = YX, B = X, C = XXY
**Output: **0**
Explanation: **XXY is not interleaving
of YX and X

**Example 2:**

**Input:
**A = XY, B = X, C = XXY
**Output: **1**
Explanation: **XXY is interleaving of
XY and X.

**Your Task:**

Complete the function **isInterleave() **which takes three strings A, B and C as input and returns true if C is an interleaving of A and B else returns false. (1 is printed by the driver code if the returned value is true, otherwise 0.)

**Expected Time Complexity:** O(N * M)

**Expected Auxiliary Space:** O(N * M)

here, N = length of A, and M = length of B

**Constraints:**

1 ≤ length of A, B, C ≤ 100

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Interleaved Strings

...