Skip to content

Commit 75a2e54

Browse files
committed
word break solution
1 parent e7a77c7 commit 75a2e54

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

word-break/robinyoon-dev.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// 문풀 해설 보고 푼 문제
2+
/**
3+
* @param {string} s
4+
* @param {string[]} wordDict
5+
* @return {boolean}
6+
*/
7+
var wordBreak = function (s, wordDict) {
8+
9+
const dp = new Array(s.length + 1).fill(false);
10+
dp[0] = true;
11+
12+
let maxLength = 0;
13+
for (let word of wordDict) {
14+
maxLength = Math.max(maxLength, word.length);
15+
}
16+
17+
for (let i = 1; i <= s.length; i++) {
18+
for (let j = Math.max(0, i - maxLength); j < i; j++) {
19+
if (dp[j] && wordDict.includes(s.substring(j, i))) {
20+
dp[i] = true;
21+
break;
22+
}
23+
}
24+
}
25+
26+
return dp[s.length];
27+
};

0 commit comments

Comments
 (0)