-
Notifications
You must be signed in to change notification settings - Fork 8
/
PiCode.js
26 lines (24 loc) · 867 Bytes
/
PiCode.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
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// fails on Performance test with timeout error: big tests where number of unique letter pairs exceeds 50.
function solution(p, q) {
let distinct_strings = [{}];
let seen = {};
p.split('').forEach((left, index) => {
let right = q[index];
if (seen[[left, right]]) return;
seen[[left, right]] = true;
distinct_strings.forEach((key) => {
let left_match = key[left];
let right_match = key[right];
if (!(left_match || right_match)) {
if (right !== left) {
distinct_strings.push(Object.assign({}, key, {[right]: true}));
}
key[left] = true;
}
});
distinct_strings = [...new Set(distinct_strings)];
});
return Math.min(...distinct_strings.map((key) => Object.keys(key).length));
}