首页 > 其他 > 详细

[LeetCode]Isomorphic Strings

时间:2015-11-30 13:00:38      阅读:280      评论:0      收藏:0      [点我收藏+]

省事用了一个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;
    }
}

 

[LeetCode]Isomorphic Strings

原文:http://www.cnblogs.com/vision-love-programming/p/5006847.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!