Program to Check whether a String is a Valid Shuffle of the other two Strings or not in Python

Program to Check whether a String is a Valid Shuffle of the two given Strings

The objective is to check whether a shuffled string is a valid shuffle of the given string inputs. Let’s say we have three string inputs, string1, string2 and shuffle. String1 and string2 are the strings whose shuffling combinations must match the string input shuffle. If the string shuffle matches the shuffle string, It’s a valid string. It’s an invalid string otherwise. 

Checking for Valid Shuffle Python Program

Checking whether the given shuffle string is a valid shuffle of the other two

The objective is to check whether the given string is a valid shuffle of the other two string inputs. To check we concatenate the two string inputs String1 and String2 which are “ABDF” and “CGFE” respectively. The shuffle string input is String3 or Shuffled String “ABDFCGFE” as shown in the figure adjacent. In order to check for valid shuffle, we concatenate String1 and String2 into a single large string of length (len(String1)+len(String2)). Now Sort the Concatenated String and the Shuffled String input. For the Shuffled String to be a Valid shuffle of the two string inputs String1 and String2, The Sorted Shuffled String must match the sorted Concatenated String. If they match, It’s a valid Shuffle, It’s invalid otherwise.

Checking for Valid Shuffle in Python

Program to check whether the shuffled string is a valid shuffle of the other two strings

Python Code
shuffle_string = input('Enter Shuffle\n')
string1 = input('Enter Str1\n')
string2 = input('Enter str2\n')
if len(shuffle_string)!=len(string1+string2):
  print('Not a valid shuffle')
else:
  newstr = string1+string2
  newstr = sorted(newstr)
  shuffle_string = sorted(shuffle_string)
  if shuffle_string != newstr:
    print('Not a valid shuffle')
  else:
    print('It is a valid shuffle')
Output
Enter Shuffle
asdasddd
Enter Str1
dasdas
Enter str2
dd
It is a valid shuffle

Explanation

For a string to be a valid shuffle of the given two string inputs, It must match with the sorted concatenation of the two given string inputs when sorted. If the sorted concatenation does not match with the sorted shuffled string, It’s considered an invalid shuffle. 

The Algorithm for the above code is as follows:

  1. Take string inputs shuffle_string, string1 and string2 using input() function.
  2. Check if the length of the shuffle_string is not equal to the length of the concatenation of string1 and string2. If not, print “Not a valid shuffle”.
  3. If equal, create a new string newstr and store the concatenation of string1 and string2 in it.
  4. Sort the newstr using sorted() function.
  5. Sort the shuffle_string using the sorted() function similarly.
  6. Check if the newstr matches with shuffle_string. If True print “It’s a valid shuffle” using print() function.
  7. Print “It’s not a valid shuffle” otherwise using the print() function.

The output for the following code is “It’s a valid shuffle” if it’s true, “It’s an invalid shuffle” otherwise.

For more solutions, click the button.