Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if characters in s can be replaced to get t, with a one-to-one mapping.
Use hash maps to track forward and reverse character mappings while scanning both strings.
Example 1:
Input: s = "egg", t = "add"
Output: true
Example 2:
Input: s = "foo", t = "bar"
Output: false
Example 3:
Input: s = "paper", t = "title"
Output: true
Algorithm Flow

Recommendation Algorithm Flow for Isomorphic Strings - Budibadu
Best Answers
java
import java.util.*;
class Solution {
public boolean isomorphic_strings(String s, String t) {
if (s.length() != t.length()) return false;
Map<Character, Character> st = new HashMap<>();
Map<Character, Character> ts = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char a = s.charAt(i), b = t.charAt(i);
if (st.containsKey(a) && st.get(a) != b) return false;
if (ts.containsKey(b) && ts.get(b) != a) return false;
st.put(a, b);
ts.put(b, a);
}
return true;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this problem.
