205 LeetCode Java: Isomorphic Strings – Easy
Problem:
Thoughts:
Solution:
class Solution {
public boolean isIsomorphic(String s, String t) {
HashMap<Character, Character> stot = new HashMap<>();
HashMap<Character, Character> ttos = new HashMap<>();
if ((s == null && t != null) || (s != null && t == null)) {
return false;
}
if (s.length() != t.length()) {
return false;
}
for (int i = 0; i < s.length(); i ++) {
char a = s.charAt(i);
char b = t.charAt(i);
if (stot.containsKey(a)) {
if (!ttos.containsKey(b) || ttos.get(b) != a) {
return false;
}
}
else {
if (ttos.containsKey(b)) {
return false;
}
stot.put(a, b);
ttos.put(b, a);
}
}
return true;
}
}Last updated