码迷,mamicode.com
首页 > 其他好文 > 详细

Isomorphic Strings

时间:2015-05-20 00:13:55      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

开始就想偏了,abcd那些字符根本不用构建,用hashmap一一对应做

ref http://blog.csdn.net/fightforyourdream/article/details/17311575

public class Solution {
    public boolean isIsomorphic(String s, String t) {
        //http://blog.csdn.net/fightforyourdream/article/details/17311575
        if(s.length()!=t.length()) return false;
        HashMap<Character, Character> hm1 = new HashMap<Character, Character>();
        HashMap<Character, Character> hm2 = new HashMap<Character, Character>();
        for(int i=0;i<s.length();i++){
            char c1 = s.charAt(i);
            char c2 = t.charAt(i);
            if(hm1.containsKey(c1)){
                if(hm1.get(c1)!=c2)
                    return false;
            }
            if(hm2.containsKey(c2)){
                if(hm2.get(c2)!=c1)
                    return false;
            }
            hm1.put(c1,c2);
            hm2.put(c2,c1);
        }
        return true;
    }
}

 

Isomorphic Strings

标签:

原文地址:http://www.cnblogs.com/jiajiaxingxing/p/4515699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!