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

205. Isomorphic Strings

时间:2016-06-09 12:14:04      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

        /*
         * 205. Isomorphic Strings
         * 2016-6-8 by Mingyang
         * 和我们上次的那个pattern一样,用hashmap做就好了
         * 这个题目可别小看,一开始的时候容易做错,ab,bb的case不过
         * 因为把a存起来的时候并不知道b,所以我们不光要判断key有没有,还要判断value有没有
         * 只有在key和value都相等的条件下才是最好的结果
         */
     public static boolean isIsomorphic(String s, String t) {
         int lens=s.length();
            int lent=t.length();
            if(lent!=lens)
              return false;
            HashMap<Character,Character> map=new HashMap<Character,Character>();
            for(int i=0;i<lens;i++){
                char temp=s.charAt(i);
                char nun=t.charAt(i);
                if(!map.containsKey(temp)&&!map.containsValue(nun)){
                    map.put(temp,nun);
                }else{
                    if(map.containsKey(temp)&&nun==(map.get(temp)))     //这里又是新加的一个判断条件,就是在map不包含temp的时候,不能判断这个条件。因为到这里的有可能只是因为map包含了nun
                      continue;
                    else
                       return false;
                }
            }
            return true;
}

 

205. Isomorphic Strings

标签:

原文地址:http://www.cnblogs.com/zmyvszk/p/5572150.html

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