-
Notifications
You must be signed in to change notification settings - Fork 0
/
205isomorphicString.js
59 lines (54 loc) · 1.47 KB
/
205isomorphicString.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* 205. Isomorphic Strings
* Given two strings s and t, determine if they are isomorphic.
* Two strings s and t are isomorphic if the characters in s can be replaced to get t.
* All occurrences of a character must be replaced with another character while preserving
* the order of characters. No two characters may map to the same character, but a
* character may map to itself.
* @param {string} s - renamed to a
* @param {string} t - renamed to b
* @return {boolean}
*/
var isIsomorphic = function (a, b) {
if (a.length !== b.length) return false;
let aX = "",
bX = "",
hashForA = [],
hashForB = [],
aCounter = 0,
bCounter = 0;
for (let i = 0; i < a.length; i++) {
let charA = a[i],
charB = b[i];
if (hashForA[charA] == undefined) {
hashForA[charA] = aCounter;
aCounter++;
}
aX += hashForA[charA] + "|";
if (hashForB[charB] == undefined) {
hashForB[charB] = bCounter;
bCounter++;
}
bX += hashForB[charB] + "|";
}
console.log({ aX, bX });
return aX == bX;
};
/* Better solution! */
var isIsomorphic = (a, b) => {
if (a.length !== b.length) return false;
let a2b = [],
b2a = [];
for (let i = 0; i < a.length; i++) {
let aChar = a[i],
bChar = b[i];
if (
(a2b[aChar] || b2a[bChar]) &&
(a2b[aChar] !== bChar || b2a[bChar] !== aChar)
)
return false;
if (!a2b[aChar]) a2b[aChar] = bChar;
if (!b2a[bChar]) b2a[bChar] = aChar;
}
return true;
};