Interleaving String Problem in JavaIt 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. ExampleInput: 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 StatementIn 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 ProblemThe problem can be solved by using the following three approaches:
Brute Force ApproachIn this approach, we must take care of the following two points:
Steps to Solve the Problem
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 ProgrammingIn 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). 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:
Steps to Solve the Problem
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 ProgrammingThe 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 TopicBulb Chain Problem in Java
|