205. Isomorphic Strings

似乎必须2个map,一个不能实现。

Time: O(k)
Space: O(256)

public class Solution {
    public boolean isIsomorphic(String s, String t) {
        if (s.length() == 0) return true;
        int[] map1 = new int[256];
        int[] map2 = new int[256];
        
        for (int i = 0; i < s.length(); i++) {
            char c1 = s.charAt(i);
            char c2 = t.charAt(i);
            if (map1[c1] == 0 && map2[c2] == 0) {
                map1[c1] = c2;
                map2[c2] = c1;
            } else if (map1[c1] == 0 || map2[c2] == 0) {
                return false;
            } else {
                if (map1[c1] != c2 || map2[c2] != c1) return false;
            }
        }
        return true;
    }
}
posted @ 2016-11-05 12:43  哇呀呀..生气啦~  阅读(67)  评论(0)    收藏  举报