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 8ea6aba commit 37aa074Copy full SHA for 37aa074
word-break/yhkee0404.scala
@@ -0,0 +1,14 @@
1
+object Solution {
2
+ def wordBreak(s: String, wordDict: List[String]): Boolean = {
3
+ val dp = Array.fill(s.length + 1)(false) // S(s, wordDict, word) = O(s.length)
4
+ dp(0) = true
5
+ for (i <- 1 to s.length) {
6
+ for (word <- wordDict) {
7
+ if (i >= word.length && dp(i - word.length) && s.substring(i - word.length, i) == word) {
8
+ dp(i) = true // T(s, wordDict, word) = O(s.length * wordDict.length * word.length)
9
+ }
10
11
12
+ dp(s.length)
13
14
+}
0 commit comments