思路:放入Map中 将字符串的字符单个提取出 未出现的放入 判断两个字符串是否保持统一的操作
public class S_205 {
public boolean isIsomorphic(String s, String t) {
if(s == null && t==null){
return true;
}
if(s.length() == 0 && t.length() == 0){
return true;
}
if(s.length() != t.length()){
return false;
}
// 保存映射关系
Map<Character,Character> map = new HashMap<>(s.length());
Set<Character> set = new HashSet<>(t.length());
char sChar;
char tChar;
for(int i = 0;i <s.length();i++){
// 分离出字符串中的字符
sChar = s.charAt(i);
tChar = t.charAt(i);
// 键如果未出现 就保存映射关系
if(!map.containsKey(sChar)){
if(set.contains(tChar)){
return false;
}else{
map.put(s.charAt(i),t.charAt(i));
set.add(tChar);
}
}
else{
if(map.get(sChar) != tChar){
return false;
}
}
}
return true;
}
}