[LeetCode]Isomorphic Strings

省事用了一个hashmap,时间复杂度是o(n2)。要是用两个hashmap时间复杂度会变为o(n)

public class Solution {
    public boolean isIsomorphic(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        int length = s.length();
        HashMap<Character, Character> map = new HashMap<Character, Character>();
        for (int i = 0; i < length; i++) {
            if (map.containsKey(s.charAt(i)) || map.containsValue(t.charAt(i))) {
                if (!map.containsKey(s.charAt(i)) || t.charAt(i) != map.get(s.charAt(i))) {
                    return false;
                } 
            } else {
                map.put(s.charAt(i), t.charAt(i));
                
            }
        }
        return true;
    }
}

 

posted @ 2015-11-30 12:44  Weizheng_Love_Coding  阅读(107)  评论(0编辑  收藏  举报