Skip to content

Commit

Permalink
add new js
Browse files Browse the repository at this point in the history
  • Loading branch information
zerob13 committed Aug 30, 2018
1 parent 1e48e87 commit 9c8edca
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/js/isomorphicStrings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isIsomorphic = function(s, t) {
if (s.length <= 1 && t.length === s.length) {
return true;
}
if (s.length != t.length) {
return false;
}
var isOk = true
var hashMap = []
var hashMap2 = []
var i = 0
for (i = 0; i < s.length; i++) {
var ts = s[i].charCodeAt(0);
var tb = t[i].charCodeAt(0);
if (hashMap[ts] == undefined) {
if (hashMap2[tb] == undefined) {
hashMap[ts] = tb
hashMap2[tb] = 1
} else {
isOk = false;
break;
}
continue;
} else {
if (hashMap[ts] == tb) {
continue;
} else {
isOk = false;
break;
}
}
}
return isOk;
};

console.log(isIsomorphic('egg', 'add'))
console.log(isIsomorphic('foo', 'bar'))
console.log(isIsomorphic('paper', 'title'))
console.log(isIsomorphic('ab', 'aa'))
console.log(isIsomorphic('ab', 'ca'))

0 comments on commit 9c8edca

Please sign in to comment.