You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1018 B

import java.util.HashMap;
import java.util.Map;
// 要考虑唯一双射
public class L0205 {
public static void main(String[] args) throws Exception {
String s = "egg";
String t = "app";
System.out.print(isIsomorphic(s, t));
}
public static boolean isIsomorphic(String s, String t) {
Map replace_chart = new HashMap();
Map replace_chars = new HashMap();
int length = s.length();
int i;
for (i = 0; i < length; i++) {
if (replace_chart.get(t.charAt(i)) == null) {
replace_chart.put(t.charAt(i), s.charAt(i));
} else if (!replace_chart.get(t.charAt(i)).equals(s.charAt(i))){
return false;
}
if (replace_chars.get(s.charAt(i)) == null) {
replace_chars.put(s.charAt(i), t.charAt(i));
} else if (!replace_chars.get(s.charAt(i)).equals(t.charAt(i))){
return false;
}
}
return true;
}
}