Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions bangdori/139.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @param {string} s
* @param {string[]} wordDict
* @return {boolean}
*/
var wordBreak = function (s, wordDict) {
const n = s.length;
const wordSet = new Set(wordDict);

let maxLength = 0;
for (const word of wordSet) {
maxLength = Math.max(maxLength, word.length);
}

const dp = Array(n + 1).fill(false);
dp[0] = true;

for (let i = 1; i <= n; i++) {
for (let j = i - 1; j >= Math.max(i - maxLength, 0); j--) {
if (dp[j] && wordSet.has(s.substring(j, i))) {
dp[i] = true;
}
}
}

return dp[n];
};