-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path205. Isomorphic Strings
31 lines (25 loc) · 1.27 KB
/
205. Isomorphic Strings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Solution {
public boolean isIsomorphic(String s, String t) {
if(s==null&&t==null)
return true;
if(s==null||t==null||s.length()!=t.length())
return false;
HashMap<Character,Character> res1=new HashMap<Character,Character>();
HashMap<Character,Character> res2=new HashMap<Character,Character>();
char[] sToArray=s.toCharArray();
char[] tToArray=t.toCharArray();
for(int i=0;i<sToArray.length;i++){
if((!res1.containsKey(sToArray[i]))&&(!res2.containsKey(tToArray[i]))){
res1.put(sToArray[i],tToArray[i]);
res2.put(tToArray[i],sToArray[i]);
}
else { if(res1.get(sToArray[i])==null||res2.get(tToArray[i])==null)//你必须要有这个判断,否则下个if会出现nullpointerexception,
//原因的话在overflow有人解释了 如果你Character和char比较,有个unopening box 过程,这个时候如果是空指针,就会出现这个exception
return false;
if(((res1.get(sToArray[i])!=tToArray[i])||(res2.get(tToArray[i])!=sToArray[i])))
return false;
}
}
return true;
}
}