Wednesday, December 2, 2015

[leetcode]Isomorphic Strings

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public class Solution {
    public boolean isIsomorphic(String s, String t) {
        if (s.length() != t.length()) return false;
        HashMap<Character, Character>lookup = new HashMap<Character, Character>();
        HashMap<Character, Character>lookup2 = new HashMap<Character, Character>();        
        for (int i = 0; i < s.length(); i++){
            char c1 = s.charAt(i);
            char c2 = t.charAt(i);
            if (lookup.containsKey(c1) && lookup.get(c1) != c2) return false;
            if (lookup2.containsKey(c2) && lookup2.get(c2) != c1) return false;
            lookup.put(c1, c2);
            lookup2.put(c2,c1);
        }
        return true;
    }
}

No comments:

Post a Comment