Check if Two Strings Are Isomorphic
Solve this Problems and t of the same length, return true if they're isomorphic — meaning the characters of s can be replaced, one-for-one and consistently, to get exactly t. No two characters may map to the same character, but a character may map to itself.
Comparing every pair of positions directly confirms consistency but costs O(n²). The faster approach walks both strings together while tracking the mapping both ways — s-to-t and t-to-s — in two hash maps. Checking both directions at every step is what catches both kinds of broken mapping in a single pass.
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
1 ≤ s.length ≤ 5 × 10⁴ - ◆
t.length == s.length - ◆
s and t consist of lowercase English letters
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
| 1 | class Solution { |
| 2 | public boolean isIsomorphic(String s, String t) { |
| 3 | Map<Character, Character> mapST = new HashMap<>(); |
| 4 | Map<Character, Character> mapTS = new HashMap<>(); |
| 5 | for (int i = 0; i < s.length(); i++) { |
| 6 | char cs = s.charAt(i); |
| 7 | char ct = t.charAt(i); |
| 8 | if (mapST.containsKey(cs) && mapST.get(cs) != ct) return false; |
| 9 | if (mapTS.containsKey(ct) && mapTS.get(ct) != cs) return false; |
| 10 | mapST.put(cs, ct); |
| 11 | mapTS.put(ct, cs); |
| 12 | } |
| 13 | return true; |
| 14 | } |
| 15 | } |
| 16 |
0eanone'e' isn't in mapST yet — no conflict possible here.
Approach & Solutions
Brute Force — Compare Every Pair of Positions
BruteTwo strings are isomorphic exactly when, for every pair of positions i and j, s[i] equals s[j] if and only if t[i] equals t[j]. Check every pair directly — if any pair disagrees between s and t, the mapping is broken. Correct, but comparing all n² pairs is quadratic.
O(n²)O(1)1class Solution {
2 public boolean isIsomorphic(String s, String t) {
3 for (int i = 0; i < s.length(); i++) {
4 for (int j = 0; j < s.length(); j++) {
5 boolean sMatch = s.charAt(i) == s.charAt(j);
6 boolean tMatch = t.charAt(i) == t.charAt(j);
7 if (sMatch != tMatch) return false;
8 }
9 }
10 return true;
11 }
12}Optimal — Two Hash Maps, One Pass
OptimalWalk both strings together, maintaining two maps: one from each s-character to the t-character it's paired with, and one going the other way. At each position, check both maps for a conflict before recording the pairing — this catches both directions of a broken mapping (two s-characters mapping to the same t-character, or one s-character mapping to two different t-characters) in a single pass.
O(n)O(1)1class Solution {
2 public boolean isIsomorphic(String s, String t) {
3 Map<Character, Character> mapST = new HashMap<>();
4 Map<Character, Character> mapTS = new HashMap<>();
5 for (int i = 0; i < s.length(); i++) {
6 char cs = s.charAt(i);
7 char ct = t.charAt(i);
8 if (mapST.containsKey(cs) && mapST.get(cs) != ct) return false;
9 if (mapTS.containsKey(ct) && mapTS.get(ct) != cs) return false;
10 mapST.put(cs, ct);
11 mapTS.put(ct, cs);
12 }
13 return true;
14 }
15}