Check If a String is a Valid Shuffle of Two Distinct Strings in Java

Exercise:

Write a Java Program to check if a string is a valid shuffle of two distinct strings.

Click Here to View the Solution!
public class shuffle {

    // check if result string is valid shuffle of string first and second
    static boolean shuffleCheck(String first, String second, String result) {

        // check length of result is same as
        // sum of result of first and second
        if(first.length() + second.length() != result.length()) {
            return false;
        }
        // variables to track each character of 3 strings
        int i = 0, j = 0, k = 0;

        // iterate through all characters of result
        while (k != result.length()) {

            // check if first character of result matches with first character of first string
            if (i < first.length() && first.charAt(i) == result.charAt(k))
                i++;

                // check if first character of result matches the first character of second string
            else if (j < second.length() && second.charAt(j) == result.charAt(k))
                j++;

                // if the character doesn't match
            else {
                return false;
            }

            // access next character of result
            k++;
        }

        // after accessing all characters of result
        // if either first or second has some characters left
        if(i < first.length() || j < second.length()) {
            return false;
        }
        return true;
    }
    public static void main(String[] args) {
        String first = "XY";
        String second = "22";
        String[] results = {"2XY2", "Y22X"};

        // call the method to check if result string is
        // shuffle of the string first and second
        for (String result : results) {
            if (shuffleCheck(first, second, result) == true) {
                System.out.println(result + " is a valid shuffle of " + first + " and " + second);
            }
            else {
                System.out.println(result + " is not a valid shuffle of " + first + " and " + second);
            }
        }
    }
}
Click Here to View the Output!
2XY2 is a valid shuffle of XY and 22
Y22X is not a valid shuffle of XY and 22
Click Here to View the Explanation!
  • This program verifies if the shuffled value(s) of two strings is correct or not.
  • The function shuffleCheck() takes three parameters; first string, second string and the shuffled value .
  • First step of this function is to make sure that the length of the result is same as the combined length of two strings.
  • In the next step, each character of result is compared with the characters of both strings one by one to check if result contains all the characters from both strings.
  • The return value of shuffleCheck() will be false if result does not contain all the characters; it is checked by comparing the values of variables i and j with the lengths of first and second string respectively.
  • Here, the result 2XY2 is a correctly shuffled option for the strings XY and 22, whereas Y22X is not valid as it altered the order of characters in the strings which should have been kept intact.

%d bloggers like this: