Straightforward Java solution with hashmap


  • 0
    K
    public class Solution {
        public boolean isIsomorphic(String s, String t) {
            int len1 = s.length();
            int len2 = t.length();
            if(len1 != len2) return false;
            HashMap<Character,Character> hm = new HashMap<>();
            for(int i = 0; i < len1; i++){
                char m = s.charAt(i);
                char n = t.charAt(i);
                if((hm.containsKey(m) && hm.get(m) != n) ||(!hm.containsKey(m) && hm.containsValue(n))) return false;
                else if(hm.containsKey(m) && hm.get(m) == n) continue;
                else if(!hm.containsKey(m) && !hm.containsValue(n)) hm.put(m,n);
            }
            return true;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.