-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructy-086-quickestConcat.js
43 lines (35 loc) · 1.05 KB
/
structy-086-quickestConcat.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
// w MEMO
const quickestConcat = (s, arr) => {
const result = _quickestConcat(s, arr, {});
return result === Infinity ? -1 : result;
};
const _quickestConcat = (s, arr, memo) => {
if (s in memo) return memo[s];
if (s.length === 0) return 0;
let min = Infinity;
for (let a of arr) {
if (s.indexOf(a) !== 0) continue;
if (s.indexOf(a) === 0)
min = Math.min(min, _quickestConcat(s.slice(a.length), arr, memo));
}
memo[s] = min + 1;
return min + 1;
};
// // wo MEMO
// const quickestConcat = (s, arr) => {
// const result = _quickestConcat(s, arr);
// console.log(result);
// return result === Infinity ? -1 : result;
// };
// const _quickestConcat = (s, arr) => {
// // console.log(s);
// if (s.length === 0) return 0;
// let min = Infinity;
// for (let a of arr) {
// if (s.indexOf(a) !== 0) continue;
// if (s.indexOf(a) === 0)
// min = Math.min(min, _quickestConcat(s.slice(a.length), arr));
// }
// return min + 1;
// };
console.log(quickestConcat("simchacindy", ["sim", "simcha", "acindy", "ch"]));