Python Program to Check if a string is a valid shuffle of two distinct strings


In Python, we can use inbuilt functions like len() and logic to check the sequence of the characters in the resultant string to know if a string is a valid shuffle of two distinct strings. A valid shuffle of two distinct strings means that a string is formed by mixing the characters of two distinct strings while maintaining the order of the characters of the two original strings. Python provides various inbuilt functions to play with strings. In this article, we will check if the string formed by shuffling the characters of two distinct strings is a valid string or not using Python.

Demonstration

Let's take an example to understand the valid shuffling of strings. Let there be two strings −

S1 : abc
S2 : def 

A valid shuffle of string s1 and s2 would be − “adbecf”, “dabecf”

A invalid shuffle of string s1 and s2 would be − “abgfcd”, “tabcde”

The strings “abgfcd” and “tabcde” when sorted do not match the sorted order of the concatenation of two original strings.

Algorithm

  • Step 1 − Check if the length of the concatenation of two distinct strings is equal to the resultant shuffled string.If the length is not equal return false else proceed to step 2.

  • Step 2 − Sort the concatenation of two original strings.

  • Step 3 − Sort the shuffled string

  • Step 4 − Check if the sorted shuffled string is equal to the sorted value of concatenated original strings. If they match return True else return false.

Example 1

In the below example, we will create the is_valid_shuffle() function which will check if the shuffled string is a valid shuffle or not. To do this we first check if the length of the shuffled string is equal to the sum of the length of the original strings s1 and s2 otherwise return false. If the string length is equal we check the sorted sequence of the concatenation of two original strings with the shuffled string. If both the strings match then it's a valid shuffle else it's not a valid shuffle.

To check the is_valid_shuffle function we pass the string as follows −

S1: abc
S2: def
Result : “adbecf”
def is_valid_shuffle(str1, str2, result):
   if len(str1) + len(str2) != len(result):
      return False

   newstr = str1+str2
   newstr = sorted(newstr)
   shuffle_string = sorted(result)
   if shuffle_string != newstr:
      return False
   else:
      return True

str1 = "abc"
str2 = "def"
result = "adbecf"
print(is_valid_shuffle(str1, str2, result))

Output

True

Example 2

Another example to check if the is_valid_shuffle identifies the invalid string we can pass the string and resultant string as follows −

S1: abc
S2: def
Result: “adebcf”
def is_valid_shuffle(str1, str2, result):
   if len(str1) + len(str2) != len(result):
      return False

   newstr = str1+str2
   newstr = sorted(newstr)
   shuffle_string = sorted(result)
   if shuffle_string != newstr:
      return False
   else:
      return True

str1 = "abc"
str2 = "def"
result = "daehfc"
print(is_valid_shuffle(str1, str2, result))

Output

False

Conclusion

In this article, we understood how to check if the string is a valid shuffle of two distinct strings or not. To check the validity we have to check if the shuffled string character length is equal to the sum of the same original string length. Then we need to check if the sorted sequence of the concatenation of two original strings is equal to the sorted value of the shuffled string then it's a valid shuffle.

Updated on: 17-Apr-2023

329 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements