205. Isomorphic Strings

正反两个字典,每次都正反检查一下

 1     public boolean isIsomorphic(String s, String t) {
 2         if(s == null || t == null) {
 3             return false;
 4         }
 5         HashMap<Character, Character> map = new HashMap<Character, Character>();
 6         HashMap<Character, Character> revMap = new HashMap<Character, Character>();
 7         for(int i = 0; i < s.length(); i++) {
 8             char c1 = s.charAt(i);
 9             char c2 = t.charAt(i);
10             if(!map.containsKey(c1)) {
11                 map.put(c1, c2);
12             } else {
13                 if(map.get(c1) != c2) {
14                     return false;
15                 }
16             }
17             if(!revMap.containsKey(c2)) {
18                 revMap.put(c2, c1);
19             } else {
20                 if(revMap.get(c2) != c1) {
21                     return false;
22                 }
23             }
24         }
25         return true;
26     }

 

posted @ 2016-07-21 08:40  warmland  阅读(140)  评论(0编辑  收藏  举报