Skip to content

Commit 20cb3a4

Browse files
authored
Create 664-strange-printer.js
1 parent 7845af3 commit 20cb3a4

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

664-strange-printer.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const strangePrinter = function(s) {
6+
let n = s.length
7+
let dp = new Array(n).fill(0).map(() => new Array(n).fill(0))
8+
9+
const help = (s, i, j) => {
10+
if (i > j) return 0
11+
if (dp[i][j] > 0) {
12+
return dp[i][j]
13+
}
14+
let res = help(s, i, j - 1) + 1
15+
for (let k = i; k < j; k++) {
16+
if (s[k] == s[j]) {
17+
res = Math.min(help(s, i, k) + help(s, k + 1, j - 1), res)
18+
}
19+
}
20+
dp[i][j] = res
21+
return res
22+
}
23+
24+
return help(s, 0, n - 1)
25+
}

0 commit comments

Comments
 (0)