Given two strings s and t, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

For example,
Given "egg""add", return true.

Given "foo""bar", return false.

Given "paper""title", return true.

132/155

思路:two hashmap一一对应字母。

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

 

posted on 2016-10-12 16:54  Machelsky  阅读(151)  评论(0编辑  收藏  举报