We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 31d2d4e commit 16fa5ccCopy full SHA for 16fa5cc
动态规划/恢复空格-面试题 17.13.js
@@ -0,0 +1,19 @@
1
+/**
2
+ * @param {string[]} dictionary
3
+ * @param {string} sentence
4
+ * @return {number}
5
+ */
6
+let respace = function (dictionary, sentence) {
7
+ let n = sentence.length
8
+ let dp = [0]
9
+ for (let i = 1; i <= n; i++) {
10
+ let min = dp[i - 1] + 1
11
+ for (let word of dictionary) {
12
+ if (sentence.substring(i - word.length, i) === word) {
13
+ min = Math.min(min, dp[i - word.length])
14
+ }
15
16
+ dp[i] = min
17
18
+ return dp[n]
19
+}
0 commit comments