From 8f7353cf2fdf327b684aac81ecf67745b653b949 Mon Sep 17 00:00:00 2001 From: Qi Jia <13632059+jackeyjia@users.noreply.github.com> Date: Wed, 7 Jul 2021 20:04:46 -0700 Subject: [PATCH] add js solution for wordBreak --- ...25\350\257\215\346\213\206\345\210\206.md" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" index b6a6242e1e..59892ef95e 100644 --- "a/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" +++ "b/problems/0139.\345\215\225\350\257\215\346\213\206\345\210\206.md" @@ -291,6 +291,27 @@ func wordBreak(s string,wordDict []string) bool { } ``` +Javascript: +```javascript +const wordBreak = (s, wordDict) => { + + let dp = Array(s.length + 1).fill(false); + dp[0] = true; + + for(let i = 0; i <= s.length; i++){ + for(let j = 0; j < wordDict.length; j++) { + if(i >= wordDict[j].length) { + if(s.slice(i - wordDict[j].length, i) === wordDict[j] && dp[i - wordDict[j].length]) { + dp[i] = true + } + } + } + } + + return dp[s.length]; +} +``` + -----------------------