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, with every character mapping to exactly one other character and preserving order.
The key constraint is one-to-one character mapping. A character from s cannot map to two different characters in t, and two different characters in s cannot map to the same character in t. Efficient solutions rely on tracking this mapping while scanning the strings once.
Approach 1: One-to-One Character Mapping (O(n) time, O(1) space)
Use two hash maps (or arrays for ASCII) to maintain a bidirectional mapping between characters in s and t. Iterate through both strings simultaneously. When you see characters s[i] and t[i], check whether an existing mapping conflicts with the current pair. If s[i] is already mapped to a different character, or t[i] is already mapped from another character, the strings are not isomorphic.
If no mapping exists yet, store the mapping in both directions. The scan finishes once every pair is validated. Each step performs constant-time hash lookups, giving O(n) time complexity and O(1) space (bounded by the character set). This approach relies heavily on hash table lookups and straightforward string traversal.
Approach 2: Unique Pattern Generation (O(n) time, O(n) space)
Another way to check isomorphism is to transform both strings into a canonical pattern representation. Iterate through each string and assign an incremental ID the first time you see a character. For example, the string paper becomes the pattern [0,1,0,3,4], and title produces the same sequence.
Build this pattern by maintaining a map from character to its first occurrence index or assigned ID. If both strings generate identical pattern arrays, they follow the same structural mapping and are therefore isomorphic. If the patterns differ at any position, the mapping rule is violated.
This technique still scans the string once, resulting in O(n) time complexity. Space usage becomes O(n) because you store the generated pattern sequence for comparison. The method is conceptually clean and useful when you want to reason about structural equivalence between strings.
Recommended for interviews: The One-to-One Character Mapping approach is what interviewers typically expect. It demonstrates understanding of bijective mappings and efficient use of hash tables with O(n) time. Pattern generation works well conceptually, but the bidirectional mapping solution is usually considered more direct and memory efficient.
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.
C++
Java
Python
C#
JavaScript
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.
C++
Java
Python
C#
JavaScript
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.
| 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. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| One-to-One Character Mapping | O(n) | O(1) | Best general solution. Efficient and commonly expected in coding interviews. |
| Unique Pattern Generation | O(n) | O(n) | Useful when comparing structural patterns or explaining the concept visually. |
Isomorphic Strings - Leetcode 205 - Python • NeetCode • 92,307 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