Problem

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.

For example,"egg" and "add" are isomorphic, "foo" and "bar" are not.
function isIsomorphic(s, t) {
    if(s==null || t==null)
        return false;
 
    if(s.length != t.length)
        return false;
 
    if(s.length==0 && t.length==0)
        return true;

  var map = {};
  for(var i=0; i<s.length; i++){
        var c1 = s.charAt(i);
        var c2 = t.charAt(i);
 
        var c = getKey(map, c2);
        if(c != null && c!=c1){
            return false;
        }else if(map[c1]!=null){
            if(c2 != map[c1])
                return false;
        }else{
            map[c1]=c2;
        }
    }
 
    return true;
}
 
// a method for getting key of a target value
function getKey(map, target){
    for (var i in map)     {
        if (map[i] == target) {
            return i;
        }
    }
    return null;
}

console.log(isIsomorphic('ett','add'));

2 comments:

  1. 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. liquidweb coupon

    ReplyDelete
  2. cas 311 28 4 - Alfa Chemistry offers an extensive catalog of Quaternary Ammonium Compounds (QACs) in a wide range of applications. Products listed on our website are either in stock or can be resynthesized within a reasonable time frame. In stock products can be shipped out within 3-5 business days upon receipt of customers' purchase order.

    ReplyDelete

Blogroll

Popular Posts