class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
"""
Determine if two strings are isomorphic, meaning there is a one-to-one mapping
between every character in s to a character in t.
Parameters:
s (str): The first string to be compared.
t (str): The second string to be compared.
Returns:
bool: True if the strings are isomorphic, False otherwise.
"""
# Dictionary to map characters from s to t.
char_map = {}
# Set to track characters from t that have already been mapped.
mapped_chars = set()
# Iterate over paired characters from s and t.
for char_s, char_t in zip(s, t):
# If we already have a mapping for the current character in s.
if char_s in char_map:
# Check if the previously mapped character matches the current character from t.
if char_map[char_s] != char_t:
# The mapping does not match, so the strings are not isomorphic.
return False
else:
# If the current character from t is already mapped to another character from s,
# then we cannot map the current char_s to char_t.
if char_t in mapped_chars:
return False
# Establish a new mapping from char_s to char_t.
char_map[char_s] = char_t
# Mark char_t as used in the mapping.
mapped_chars.add(char_t)
# All character mappings are consistent; the strings are isomorphic.
return True
Summary of Techniques and Approaches:
Mapping with Dictionaries: Using a dictionary to track the mapping between elements (in this case, characters) allows an efficient O(n) solution. This technique is widely applicable for problems that require tracking unique pairings or relationships.
Tracking Mapped Values with a Set: Using a set to record already-mapped values helps quickly check for conflicts and duplicate assignments. This pattern is useful when you need to enforce uniqueness constraints.
Single-Pass Iteration: The solution iterates over both strings simultaneously in a single loop, ensuring optimal time complexity. Whenever possible, aim for single-pass algorithms to reduce overhead and simplify logic.
Validation during Iteration: The method performs validation (ensuring consistent mapping) within the loop itself, allowing for early exits when an inconsistency is detected. This approach is effective in many scenarios where constraints must be maintained in real time.
General Applicability: These techniques can be applied to many problems that involve one-to-one mappings, pairings, or relationship validations, such as checking bijections, deduplication tasks, and even graph isomorphism problems. The key is to identify when a direct mapping or pairing exists and how you can efficiently maintain and verify it.