Practice Problem
Isomorphic Strings
Difficulty: Easy
Determine if two strings are isomorphic, where each character in one string can be mapped to exactly one character in the other.
Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t, with the following rules:
- Each character in
smaps to exactly one character int. - No two characters in
smap to the same character int. - The order of characters is preserved.
Examples
Example 1:
Input: s = "egg", t = "add"
Output: true
Explanation: 'e' -> 'a', 'g' -> 'd'. The mapping is consistent and one-to-one.Example 2:
Input: s = "foo", t = "bar"
Output: false
Explanation: 'f' -> 'b', 'o' -> 'a', but then the second 'o' should map to 'a', not 'r'. Wait, actually 'o' maps to 'a' first time, but 'o' at index 2 would need to map to 'r'. Contradiction.Example 3:
Input: s = "paper", t = "title"
Output: true
Explanation: 'p' -> 't', 'a' -> 'i', 'e' -> 'l', 'r' -> 'e'. Consistent one-to-one mapping.Constraints
1 <= s.length <= 5 * 10^4s.length == t.lengthsandtconsist of any valid ASCII characters.
Expected Complexity
- Time: O(n)
- Space: O(1). at most 128 ASCII character mappings
EASY
Arrays
Hash Map / Dictionary
Strings
Beginner
0 views
Solution
Hints
Hint 1
Hint 2
Premium
Hint 3
Premium
This section is available for CodeSnatch Premium members only.
