-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathisomorphicStrings.js
More file actions
30 lines (28 loc) · 686 Bytes
/
isomorphicStrings.js
File metadata and controls
30 lines (28 loc) · 686 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function isIsomorphic(s, t) {
if(s.length !== t.length){
return false;
}
let sMap = new Map();
let tMap = new Map();
for (let i = 0; i < s.length; i++) {
if(sMap.has(s[i])) {
if(t[i] !== sMap.get(s[i])) {
return false;
}
} else {
sMap.set(s[i], t[i]);
}
if(tMap.has(t[i])) {
if(s[i] !== tMap.get(t[i])) {
return false;
}
} else {
tMap.set(t[i], s[i]);
}
}
return true;
}
const s1 = "dad", t1 = "mom";
const s2 = "zoo", t2 = "cat";
console.log(isIsomorphic(s1, t1));
console.log(isIsomorphic(s2, t2));