205.Isomorphic Strings
TestCase
aa vs ab
class Solution {
public boolean isIsomorphic(String s, String t) {
int n1 = s.length();
int n2 = t.length();
if(n1 != n2)
return false;
Map<Character, Character> map1 = new HashMap<>();
Map<Character, Character> map2 = new HashMap<>();
for(int i = 0; i < n1; i++){
char c1 = s.charAt(i);
char c2 = t.charAt(i);
if(map1.containsKey(c1)){
if(map1.get(c1) != c2)
return false;
}
else if(map2.containsKey(c2)){
if(map2.get(c2) != c1)
return false;
}
else{
map1.put(c1, c2);
map2.put(c2, c1);
}
}
return true;
}
}