Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Check if a string is a valid shuffle of two distinct strings
In Python, we can check if a string is a valid shuffle of two distinct strings by comparing the sorted characters. A valid shuffle means that a string is formed by mixing characters from two distinct strings while maintaining the relative order of characters from each original string.
What is a Valid Shuffle?
A valid shuffle combines characters from two strings without adding, removing, or changing any characters. Let's see some examples ?
S1: "abc" S2: "def"
Valid shuffles: "adbecf", "dabecf", "abdefc"
Invalid shuffles: "abgfcd" (contains 'g'), "tabcde" (contains 't')
Algorithm
Step 1: Check if the length of the result equals the sum of lengths of both strings
Step 2: Sort the concatenation of the two original strings
Step 3: Sort the shuffled string
Step 4: Compare both sorted strings for equality
Method 1: Using Sorted Comparison
The simplest approach is to check if both strings contain exactly the same characters ?
def is_valid_shuffle(str1, str2, result):
# Check length first
if len(str1) + len(str2) != len(result):
return False
# Compare sorted characters
original_chars = sorted(str1 + str2)
result_chars = sorted(result)
return original_chars == result_chars
# Test with valid shuffle
str1 = "abc"
str2 = "def"
result = "adbecf"
print(f"Is '{result}' a valid shuffle of '{str1}' and '{str2}'?")
print(is_valid_shuffle(str1, str2, result))
Is 'adbecf' a valid shuffle of 'abc' and 'def'? True
Method 2: Using Character Frequency Count
An alternative approach using dictionaries to count character frequencies ?
from collections import Counter
def is_valid_shuffle_counter(str1, str2, result):
# Check length
if len(str1) + len(str2) != len(result):
return False
# Count characters in original strings
original_count = Counter(str1 + str2)
result_count = Counter(result)
return original_count == result_count
# Test with invalid shuffle
str1 = "abc"
str2 = "def"
result = "daehfc" # Contains 'h' which is not in original strings
print(f"Is '{result}' a valid shuffle of '{str1}' and '{str2}'?")
print(is_valid_shuffle_counter(str1, str2, result))
Is 'daehfc' a valid shuffle of 'abc' and 'def'? False
Testing Multiple Cases
Let's test both valid and invalid cases ?
def is_valid_shuffle(str1, str2, result):
if len(str1) + len(str2) != len(result):
return False
return sorted(str1 + str2) == sorted(result)
# Test cases
test_cases = [
("abc", "def", "adbecf", True), # Valid
("abc", "def", "dabecf", True), # Valid
("abc", "def", "abcdef", True), # Valid
("abc", "def", "daehfc", False), # Invalid - contains 'h'
("abc", "def", "abcde", False), # Invalid - missing 'f'
("hello", "world", "hweolrllod", True), # Valid
]
for str1, str2, result, expected in test_cases:
actual = is_valid_shuffle(str1, str2, result)
status = "?" if actual == expected else "?"
print(f"{status} '{result}' shuffle of '{str1}' + '{str2}': {actual}")
? 'adbecf' shuffle of 'abc' + 'def': True ? 'dabecf' shuffle of 'abc' + 'def': True ? 'abcdef' shuffle of 'abc' + 'def': True ? 'daehfc' shuffle of 'abc' + 'def': False ? 'abcde' shuffle of 'abc' + 'def': False ? 'hweolrllod' shuffle of 'hello' + 'world': True
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Sorted Comparison | O(n log n) | O(n) | Simple implementation |
| Counter | O(n) | O(k) | Large strings with few unique chars |
Conclusion
A valid shuffle contains exactly the same characters as the original strings combined. Use sorted comparison for simplicity, or Counter for better time complexity with large strings.
---