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
String Transforms Into Another String in Python
Sometimes we need to check if we can transform one string into another by converting all occurrences of specific characters. This problem involves character mapping and transformation rules to determine if str1 can be converted to str2.
In one conversion, we can convert all occurrences of one character in str1 to any other lowercase English character. The key constraint is that we need at least one "free" character (not used in str2) to perform intermediate conversions.
Problem Example
Given str1 = "aabcc" and str2 = "ccdee", we can transform str1 to str2 ?
Convert 'c' to 'e': "aabee"
Convert 'b' to 'd': "aadee"
Convert 'a' to 'c': "ccdee"
Algorithm Approach
The solution uses two key concepts ?
Character Mapping: Each character in str1 must map to exactly one character in str2
Free Character Requirement: We need at least one unused character for intermediate transformations, unless str1 equals str2
Implementation
def can_convert(str1, str2):
if len(str1) != len(str2):
return False
if str1 == str2:
return True
# Create mapping from str1 to str2
char_map = {}
for c1, c2 in zip(str1, str2):
if c1 in char_map:
if char_map[c1] != c2:
return False
else:
char_map[c1] = c2
# Check if we have a free character
unique_chars_str2 = set(str2)
return len(unique_chars_str2) < 26
# Test the function
print(can_convert("aabcc", "ccdee"))
print(can_convert("leetcode", "codeleet"))
print(can_convert("abcdefghijklmnopqrstuvwxyz", "bcdefghijklmnopqrstuvwxyza"))
True False False
How It Works
The algorithm follows these steps ?
Length Check: Both strings must have the same length
Identity Check: If strings are identical, no conversion is needed
Mapping Validation: Each character in str1 must consistently map to the same character in str2
Free Character Check: We need at least one character (out of 26) not used in str2 for intermediate conversions
Edge Cases
def can_convert(str1, str2):
if len(str1) != len(str2):
return False
if str1 == str2:
return True
char_map = {}
for c1, c2 in zip(str1, str2):
if c1 in char_map:
if char_map[c1] != c2:
return False
else:
char_map[c1] = c2
unique_chars_str2 = set(str2)
return len(unique_chars_str2) < 26
# Edge case: All 26 characters used in str2
print(can_convert("a", "b")) # True - has free characters
print(can_convert("aa", "bb")) # True - can convert directly
print(can_convert("ab", "ba")) # True - can use intermediate character
True True True
Key Points
Consistent Mapping: Each character in str1 must always map to the same character in str2
Free Character Rule: If all 26 lowercase letters appear in str2, transformation becomes impossible (except when str1 == str2)
Order Independence: The actual order of transformations doesn't matter for the final result
Conclusion
To transform one string into another, ensure consistent character mapping and verify that at least one character remains unused in the target string. This free character enables intermediate conversions when needed.
