Closed congr closed 5 years ago
class Solution {
public boolean isIsomorphic(String s, String t) {
Map<Character, Character> smap = new HashMap();
Map<Character, Character> tmap = new HashMap();
for (int i = 0; i < s.length(); i++) {
char a = smap.getOrDefault(s.charAt(i), ' ');
char b = tmap.getOrDefault(t.charAt(i), ' ');
if (a == ' ' && b == ' ');
else if (a != t.charAt(i) || b != s.charAt(i)) return false;
smap.put(s.charAt(i), t.charAt(i));
tmap.put(t.charAt(i), s.charAt(i));
}
return true;
}
}
class Solution {
public boolean isIsomorphic(String s, String t) {
Map<Character, Integer> smap = new HashMap<>();
Map<Character, Integer> tmap = new HashMap<>();
for (Integer i = 0; i < s.length(); i++) { // !!! Integer
if (smap.put(s.charAt(i), i) != tmap.put(t.charAt(i), i))
return false;
}
return true;
}
}
https://leetcode.com/problems/isomorphic-strings/