/*
思路: 利用hashmap 建立一个对应关系 s里面的每个位置对应t里面相同的位置 如出现重复并且对应不同则为false 要想避免s== “ab“ t == ”aa“
这种情况 则 反向t 对应s一起判断即可
*/
class Solution {
public boolean isIsomorphic(String s, String t) {
if(s.length() != t.length()) return false;
Map<Character, Character> mapst = new HashMap<>();
Map<Character, Character> mapts = new HashMap<>();
for(int i = 0; i < s.length(); i++) {
char sChar = s.charAt(i);
char tChar = t.charAt(i);
if(mapst.containsKey(sChar)) {
if(mapst.get(sChar) != tChar) {
return false;
}
} else {
mapst.put(sChar, tChar);
}
if(mapts.containsKey(tChar)) {
if(mapts.get(tChar) != sChar) {
return false;
}
} else {
mapts.put(tChar, sChar);
}
}
return true;
}
}
Isomorphic Strings
最新推荐文章于 2022-02-21 17:02:54 发布