Given two strings s and t, determine if they are isomorphic.
Two strings s and t are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
Example 1:
Input: s = "egg", t = "add"
Output: true
Explanation:
The strings s and t can be made identical by:
'e' to 'a'.'g' to 'd'.Example 2:
Input: s = "foo", t = "bar"
Output: false
Explanation:
The strings s and t can not be made identical as 'o' needs to be mapped to both 'a' and 'r'.
Example 3:
Input: s = "paper", t = "title"
Output: true
Constraints:
1 <= s.length <= 5 * 104t.length == s.lengths and t consist of any valid ascii character.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are given two secret messages and want to check if you can completely translate one into the other by swapping every letter from the first message with a matching letter in the second message. The brute force strategy involves testing every single possible way to pair up the letters from both messages and checking if any of those combinations successfully turn the first message into the second one.
Here's how the algorithm would work step-by-step:
def isIsomorphic(source_string, target_string):
import itertools
if len(source_string) != len(target_string):
return False
unique_source_chars = list(set(source_string))
unique_target_chars = list(set(target_string))
if len(unique_source_chars) != len(unique_target_chars):
return False
# Generate all possible letter mappings to exhaustively test every combination strategy
for target_permutation in itertools.permutations(unique_target_chars, len(unique_source_chars)):
mapping_dictionary = dict(zip(unique_source_chars, target_permutation))
transformed_chars = []
for character in source_string:
transformed_chars.append(mapping_dictionary[character])
# Verify if the current mapping successfully transforms the entire source into the target
if "".join(transformed_chars) == target_string:
return True
# If all brute force mapping combinations fail, the strings cannot be isomorphic
return FalseTo check if two things match in a special structural way, we can pair up their individual pieces one by one as we move along. By keeping track of who is paired with who, we can immediately catch if someone tries to pair with two different partners or if a partner gets stolen.
Here's how the algorithm would work step-by-step:
def is_isomorphic(first_string, second_string):
first_to_second_map = {}
second_to_first_map = {}
for char_index in range(len(first_string)):
char_from_first = first_string[char_index]
char_from_second = second_string[char_index]
# We must ensure a consistent one-to-one mapping in both directions.
if char_from_first in first_to_second_map and first_to_second_map[char_from_first] != char_from_second:
return False
# Prevent two distinct characters from mapping to the same target character.
if char_from_second in second_to_first_map and second_to_first_map[char_from_second] != char_from_first:
return False
first_to_second_map[char_from_first] = char_from_second
second_to_first_map[char_from_second] = char_from_first
return True| Case | How to Handle |
|---|---|
| Empty strings or strings of length one | Return true immediately because empty strings are trivially isomorphic and single characters always map correctly. |
| Strings of unequal length | Return false right away since strings must have the same length to establish a one-to-one character mapping. |
| Many-to-one mapping attempt where two different characters in s map to the same character in t | Maintain a reverse mapping or set of used target characters to detect collisions and return false. |
| Strings with all identical characters | Verify that a single character maps consistently to another single character without triggering mapping conflicts. |
| Strings with completely disjoint character sets | Process each unique character pair sequentially to ensure correct bi-directional mapping is established. |
| Maximum length strings with large character sets like Unicode | Use hash maps or fixed-size frequency arrays to achieve optimal linear time complexity and scale efficiently. |
| Strings that initially map correctly but diverge at the final character | Iterate through the entire length of the strings to ensure consistency until the last position. |