forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
29 lines (29 loc) · 951 Bytes
/
Solution.ts
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
function minimizeConcatenatedLength(words: string[]): number {
const n = words.length;
const f: number[][][] = Array(n)
.fill(0)
.map(() =>
Array(26)
.fill(0)
.map(() => Array(26).fill(0)),
);
const dfs = (i: number, a: number, b: number): number => {
if (i >= n) {
return 0;
}
if (f[i][a][b] > 0) {
return f[i][a][b];
}
const s = words[i];
const m = s.length;
const x =
dfs(i + 1, a, s[m - 1].charCodeAt(0) - 97) - (s[0].charCodeAt(0) - 97 === b ? 1 : 0);
const y =
dfs(i + 1, s[0].charCodeAt(0) - 97, b) - (s[m - 1].charCodeAt(0) - 97 === a ? 1 : 0);
return (f[i][a][b] = Math.min(x + m, y + m));
};
return (
words[0].length +
dfs(1, words[0][0].charCodeAt(0) - 97, words[0][words[0].length - 1].charCodeAt(0) - 97)
);
}