Isomorphic Strings 题目:https://leetcode.com/problems/isomorphic-strings/
同构字符串:
建立一个map 实现一个字符串到另一个字符串的映射,用set存储第二个字符串,避免出现多对一。
public static void main(String[] args) {
// String s="egg";
// String t="add";
String s="foo";
String t="bar";
boolean isomorphic = isIsomorphic(s, t);
System.out.println(isomorphic);
}
public static boolean isIsomorphic(String s, String t) {
Map<Character,Character> map=new HashMap<>();
Set<Character> set=new HashSet<>();
if(s.length()!=t.length()){
return false;
}
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
char d = t.charAt(i);
if(map.containsKey(c)){
if(map.get(c)==d){
}else{
return false;
}
}else{
if(set.contains(d)){
return false;
}else{
map.put(c,d);
set.add(d);
}
}
}
return true;
}