-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsolution.js
53 lines (41 loc) · 1.12 KB
/
solution.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
const canReconfigure = (from, to) => {
if (from.length !== to.length) {
return false;
}
const checkerStore = {
from: {},
to: {}
};
for (let i = 0; i < from.length; i++) {
const fromLetter = from[i];
const toLetter = to[i];
if (
(fromLetter in checkerStore.from && checkerStore.from[fromLetter] !== toLetter) ||
(toLetter in checkerStore.to && checkerStore.to[toLetter] !== fromLetter)
) {
return false;
}
checkerStore.from[fromLetter] = toLetter;
checkerStore.to[toLetter] = fromLetter;
}
return true;
};
const canReconfigureAlt = (from, to) => {
if (from.length !== to.length) {
return false;
}
const checkerStore = {};
for (let i = 0; i < from.length; i++) {
const fromLetter = from[i];
const toLetter = to[i];
if (
(fromLetter in checkerStore && checkerStore[fromLetter] !== toLetter) ||
(Object.values(checkerStore).includes(toLetter) && checkerStore[fromLetter] !== toLetter)
) {
return false;
}
checkerStore[fromLetter] = toLetter;
}
return true;
};
export { canReconfigure, canReconfigureAlt };