Isomorphic Strings

开始就想偏了,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;

    }

}

 

你可能感兴趣的:(String)