-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path205-isomorphic-strings
More file actions
29 lines (28 loc) · 843 Bytes
/
205-isomorphic-strings
File metadata and controls
29 lines (28 loc) · 843 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
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isIsomorphic = function(s, t) {
let codex = {};
let codexValues = [];
for (let i = 0; i < s.length; ++i) {
const targetChar = s.charAt(i);
if (codex.hasOwnProperty(targetChar)) {
if (t.charAt(i) !== codex[targetChar]) return false
else continue;
}
else if (codexValues.includes(t.charAt(i))) return false
else {
codex[targetChar] = t.charAt(i);
codexValues.push(t.charAt(i));
}
};
return true;
};
/**
* Notes:
* Two strings s and t are isomorphic if the characters in s can be replaced to get t
* Example: s = "egg", t = "add" - true
* Example: s = "foo", t = "bar" - false, because the first instance of o maps to a, and may not then map to r
*/