Javatpoint Logo
Javatpoint Logo

Interleaving String Problem in Java

It is a very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, Adobe, Apple, Infosys, Microsoft, Yahoo, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to discuss what is interleaving string and how to check if the given string is interleaving or not. We will also create Java program for the same with different approaches and logic

What is interleaving string?

The string Str3 is said to be interleaving Str1 and Str2 if it (Str3) contains all characters of St1 and Str2. Remember that the order of all characters in individual strings is preserved.

Example

Input: Str1 = "aabcc", Str2 = "sbbca" and Str3 = "aasbbcbcac"

Output: String 3 is an interleaving string.

Explanation: "aa" (from Str1) + "sbbc" (from Str2) + "bc" (from Str1) + "a" (from Str2) + "c" (from Str1)

Let's see another example.

Input: Str1 = "ttbhh", Str2 = "kbbht", Str3 = "ttkbbbthhh"

Output: String 3 is not an interleaving string.

Explanation: It is not possible to get Str3 by interleaving Str1 and Str2.

Problem Statement

In this problem, we have given three strings say Str1, Str2 and Str3, write a program that checks whether Str3 is an interleaving of Str1 and Str2.

Solution to the Problem

The problem can be solved by using the following three approaches:

  • Brute Force Approach
  • Using 2D Dynamic Programming
  • Using 1D Dynamic Programming

Brute Force Approach

In this approach, we must take care of the following two points:

  • If the first character of Str3 matches the first character of Str1, we move to the next character ahead in Str1 and Str3 and recursively call the function.
  • If the first character of Str3 matches the first character of Str2, we move to the next character ahead in Str2 and Str3 and recursively call the function.

Steps to Solve the Problem

  1. First, we check the base case. If Str1, Str2, and Str3 are empty return true as empty Str3 is an interleaving of Str1 and Str2.
  2. Again, check if Str3 is empty and either Str1 and Str2 is not empty, return false. It means length(Str3) is smaller than length(Str1) + length(Str2).
  3. If the above two cases are not matched, move ahead with the following two conditions:
    1. If Str3[0] == Str1[0], then check for Str1[1…], Str2, Str3[1...]
    2. If Str3[0] == Str2[0]then check for Str1, Str2[1…], Str3[1…]
    3. If any of the two possibilities becomes true, return true, else return false.

Let's implement the above approach in a function.

Complexity

The time complexity of the above approach is O(n2) because for every character of Str3 there may be two options. The space complexity is O(1) because in this approach we have not considered recursion stack space.

Using 2D Dynamic Programming

In this approach we will build a two-dimensional DP table, according to the drawn path displayed as shown below. Here certain order must be maintained so that we can move right or down in order to create a valid path. It is the reason DP[i][j] is depending on DP[i - 1][j] and DP[i][j - 1].

In order to get the DP[i][j], store true or false in the DP table, created above. Here, DP[i][j] denotes that str3.substr(0, i+j) can be formed by str1.substr(0, i) interleaving str2.substr(0, j).

Interleaving String Problem in Java

Let's see the implementation of the approach.

First, create a 2D Boolean array named DP][]. In this array DP[i][j] indicates if it is possible to obtain a substring of length (i+j+2). It is a prefix of Str3 by some interleaving of prefixes of strings Str1 and Str2 having lengths (i+1) and (j+1), respectively.

In short, DP table denotes if str3 is interleaving at (i+j)-th index when S1 is at i-th index, and S2 is at j-th index. 0th index means an empty string. 0th index denotes the empty string. We can conclude that:

  • If str1 and str2 is empty, then str3 will also empty. Therefore, we can consider it as interleaving.
  • If only str1 is an empty string, and if the previous str2 position is interleaving and the current str2 position character is equal to str3 current position character, it will be considered as interleaving.
  • The same applies if str2 is empty. when both str1 and str2 are not empty, then whenever we arrive at i, j from i-1, j, and if i-1, j is already interleaving and i and current str3 position are equal, then they are interleaving string. If we arrive at i, j from i, j-1, and if i, j-1 is already interleaving and j and current str3 position equal then also it is interleaving.

Steps to Solve the Problem

  • Create a 2D boolean DP[] array of size m+1 and n+1. Where m and n are the lengths of Str1 and Str2, respectively.
  • Now fill the DP table by comparing the characters of Str1, Str2 with Str3 accordingly.
  • Return DP[m][n]

InterleavingStringExample2.java

Output:

true

Complexity

The time and space complexity of the above approach is O(m*n). where m and n are the length of the strings.

Using 1D Dynamic Programming

The approach is the same as the above approach. The only difference is that we have used only 1D array to store the results of the prefixes of the specified string that we processed further. The advantage of using 1D array is that we need to update only the array's element dp[i] when needed using dp[i-1] and the previous value of dp[i].

Let's implement the above approach in a Java program.

InterleavingStringExample3.java

Output:

true

Complexity Analysis

The time complexity of the above approach is O(m*n) because dp[] array of size m*n filled mn times. The space complexity is O(n) where n is the length of the string str1.


Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA