0205. Isomorphic Strings
Description
**Input:** s = "egg", t = "add"
**Output:** true**Input:** s = "foo", t = "bar"
**Output:** false**Input:** s = "paper", t = "title"
**Output:** trueac
Last updated
**Input:** s = "egg", t = "add"
**Output:** true**Input:** s = "foo", t = "bar"
**Output:** false**Input:** s = "paper", t = "title"
**Output:** trueLast updated
class Solution {
public boolean isIsomorphic(String s, String t) {
// edge cases
if (s.length() != t.length()) return false;
Map<Character, Character> map = new HashMap<>();
Set<Character> set = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
char cs = s.charAt(i);
char ct = t.charAt(i);
if (!map.containsKey(cs)) {
if (set.contains(ct)) return false;
map.put(cs, ct);
set.add(ct);
} else {
if (map.get(cs) != ct) return false;
}
}
return true;
}
}