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.Problem Overview: Given two strings s and t, determine whether they are isomorphic. Two strings are isomorphic if characters in s can be replaced to get t while preserving order. Each character must map to exactly one character, and no two characters can map to the same target character.
Approach 1: One-to-One Character Mapping (O(n) time, O(k) space)
This approach enforces a strict one-to-one mapping between characters in the two strings using hash tables. Iterate through both strings simultaneously and maintain a mapping from s[i] to t[i]. If the character in s was seen before, verify that it maps to the same character in t. To prevent two characters mapping to the same target, maintain a second set or reverse map to track already-mapped characters.
The key insight is that isomorphic strings behave like a bijection: every source character must consistently map to one target character. Hash lookups make each validation constant time, so the entire scan runs in O(n). Space complexity is O(k), where k is the number of unique characters (bounded by the character set). This method relies heavily on a Hash Table for constant-time mapping and works well for general String problems involving character relationships.
Approach 2: Unique Pattern Generation (O(n) time, O(n) space)
Instead of directly mapping characters, convert each string into a structural pattern that represents how characters appear. Traverse the string and assign each new character an increasing index based on its first occurrence. For example, the string egg becomes the pattern [0,1,1], and add produces the same pattern.
Generate this pattern for both strings and compare the resulting sequences. If the patterns match, the strings are isomorphic. This works because the pattern encodes the structure of character repetition and ordering. Pattern generation uses a hash map to track first occurrences and builds an array of indices, giving O(n) time and O(n) space.
This method is conceptually clean and helpful when reasoning about structural equivalence between strings. It also generalizes well to problems where you compare patterns rather than raw characters.
Recommended for interviews: The one-to-one character mapping approach is what interviewers typically expect. It directly models the bijection requirement and demonstrates comfort with hash table lookups and constraint validation during iteration. Mentioning the pattern-generation method shows deeper understanding, but the mapping solution is simpler, uses less memory, and is the most common production implementation.
This approach involves using two hash maps (or dictionaries) to track the character mappings from string s to string t and vice versa. We iterate over the characters of both strings and update the mappings. If at any point the expected character mapping does not match, we conclude that the strings are not isomorphic.
Using arrays indexed by ASCII values, we map each character from string s to string t and vice versa. We increment the indices compared by one to differentiate from default values. If a mismatch is found during comparison, we return false immediately.
Time Complexity: O(n) where n is the length of the strings (which are equal).
Space Complexity: O(1), as the size of the map is constant (256 character ASCII set).
Generate a unique pattern for each string by numbering the characters according to their first appearance. Compare character patterns of the two strings. If the patterns match, the strings are isomorphic.
Generate a number pattern for each string based on character occurrences using a mapping array. Compare these patterns; identical patterns indicate that the strings are isomorphic.
Time Complexity: O(n), due to single-pass traversal for number pattern generation.
Space Complexity: O(1), as the pattern size remains constant with 256 ASCII values.
We can use two hash tables or arrays d_1 and d_2 to record the character mapping relationship between s and t.
Traverse s and t, if the corresponding character mapping relationships in d_1 and d_2 are different, return false, otherwise update the corresponding character mapping relationships in d_1 and d_2. After the traversal is complete, it means that s and t are isomorphic, and return true.
The time complexity is O(n) and the space complexity is O(C). Where n is the length of the string s; and C is the size of the character set, which is C = 256 in this problem.
| Approach | Complexity |
|---|---|
| One-to-One Character Mapping | Time Complexity: O(n) where n is the length of the strings (which are equal). |
| Unique Pattern Generation | Time Complexity: O(n), due to single-pass traversal for number pattern generation. |
| Hash Table or Array | — |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| One-to-One Character Mapping | O(n) | O(k) | General case; best practical approach using hash maps to enforce bijection |
| Unique Pattern Generation | O(n) | O(n) | When comparing structural patterns of strings or explaining the concept visually |
Isomorphic Strings - Leetcode 205 - Python • NeetCode • 109,152 views views
Watch 9 more video solutions →Practice Isomorphic Strings with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor