English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java input/output (I)/O)

Java Reader/Writer

Other Java topics

Java program to check if a string is a valid shuffle (rearrangement) of two strings

Comprehensive Java Examples

In this instance, we will check if a string is a valid rearrangement (shuffle) of two other strings in Java.

To understand this example, you should be familiar with the followingJava programmingTopic:

Example: Check if a string is a valid rearrangement of two other strings

class Main {
  //check if the result string is a valid shuffle of the first and second strings
  static boolean shuffleCheck(String first, String second, String result) {
    //check if the length of the result matches
    //the sum of the lengths of the first and second results
    if(first.length() + second.length() != result.length()) {
      return false;
    }
    //track3variables for each character of the two strings
    int i = 0, j = 0, k = 0;
    //traverse all characters of the result
    while (k != result.length()) {
      //check if the first character of the result matches the first character of the first string
      if (i < first.length() && first.charAt(i) == result.charAt(k)) {
        i++;
      //check if the first character of the result matches the first character of the second string
      } else if (j < second.length() && second.charAt(j) == result.charAt(k)) {
        j++;
      //if the characters do not match
      } else {
        return false;
      }
      //access the next character of the result
      k++;
    }
    //after accessing all characters of the result
     //if there are some characters left in the first or second
    if(i < first.length() || j < second.length()) {
      return false;
    }
    return true;
  }
  public static void main(String[] args) {
    String first = "XY";
    String second = "12";
    String[] results = {"1XY2",	"Y12X"};
    //The method is called to check if the result string is
    //a mixture of first and second
    for (String result : results) {
      if (shuffleCheck(first, second, result) == true) {
        System.out.println(result + " is" + first + " and" + second + "Valid Reorganization");
      }
      else {
        System.out.println(result + " is not" + first + " and" + second+ "Valid Reorganization");
      }
    }
  }
}

Output Result

1XY2 is XY and 12 Valid Reorganization
Y12X is not XY and 12 Valid Reorganization

In the above example, we have a string array named results. It contains two strings:1XY2and Y12X. We are checking if the two strings are valid strings first(XY) and second(12)。

Here, the program says1XY2is a valid shuffle XY and12. But, Y12X This is not a valid shuffle.

This is because Y12X has changed the order of the string to XY. Here, Y is used for the previous X. Therefore, as an effective reorganization, the order of the string should be maintained.

Comprehensive Java Examples