Skip to content

Commit 37aa074

Browse files
authored
word break solution
1 parent 8ea6aba commit 37aa074

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

word-break/yhkee0404.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)