w3resource

Java: Check whether two strings are interleaving of a specified string

Java String: Exercise-36 with Solution

Write a Java program to check whether two strings interlive of a given string. Assuming that unique characters are present in both strings.

Visual Presentation:

Java String Exercises: Check whether two strings are interleaving of a specified string

Sample Solution:

Java Code:

// Import necessary Java utilities.
import java.util.*;

// Define a class named Main.
public class Main {
    
    // The main method to start the execution of the program.
    public static void main(String args[]) {
        // Define three strings.
        String str1 = "MNO";
        String str2 = "PQ";
        String str3 = "PMQNO";
        
        // Print the given string and interleaving strings.
        System.out.println("The given string is: " + str3);
        System.out.println("The interleaving strings are " + str1 + " and " + str2);
        
        // Check if the given string is interleaving of str1 and str2, then print the result.
        System.out.println("The given string is interleaving: " + checkInterleaved(str1, str2, str3));
    }
    
    // Method to check if a string is interleaving of two other strings.
    private static boolean checkInterleaved(String str1, String str2, String CheckInString) {
        int i = 0, j = 0, k = 0;
        
        // If the length of concatenated str1 and str2 doesn't match the length of the checked string, return false.
        if (str1.length() + str2.length() != CheckInString.length()) {
            return false;
        }
        
        // Loop through the checked string.
        while (k < CheckInString.length()) {
            // Check if character at index i in str1 matches with character at index k in CheckInString.
            if (i < str1.length() && str1.charAt(i) == CheckInString.charAt(k)) {
                i++;
            }
            // Check if character at index j in str2 matches with character at index k in CheckInString.
            else if (j < str2.length() && str2.charAt(j) == CheckInString.charAt(k)) {
                j++;
            } else {
                return false;
            }
            k++;
        }
        
        // If lengths of str1, str2, and CheckInString don't match, return false.
        if (!(i == str1.length() && j == str2.length() && k == CheckInString.length())) {
            return false;
        }
        
        return true;
    }
}

Sample Output:

The given string is: PMQNO
The interleaving strings are MNO and PQ
The given string is interleaving: true

The given string is: PNQMO
The interleaving strings are MNO and PQ
The given string is interleaving: false

N.B.: In the 2nd case, in interleave string, N comes before M and in original string str1 = MNO (N comes after M) So it is not interleaved.

Flowchart:

Flowchart: Java String Exercises - Check whether two strings are interliving of a specified string

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to print all permutations of a given string with repetition.
Next: Write a Java program to find length of the longest substring of a given string without repeating characters.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/string/java-string-exercise-36.php