Skip to content

Commit 1d55dbb

Browse files
committed
Create 0139-word-break.swift
1 parent 745097a commit 1d55dbb

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

swift/0139-word-break.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
func wordBreak(_ s: String, _ wordDict: [String]) -> Bool {
3+
var s = Array(s)
4+
var dp = [Bool](repeating: false, count: s.count + 1)
5+
dp[s.count] = true
6+
7+
for i in stride(from: s.count - 1, to: -1, by: -1) {
8+
for w in wordDict {
9+
if i + w.count <= s.count && String(s[i..<(i + w.count)]) == w {
10+
dp[i] = dp[i + w.count]
11+
}
12+
if dp[i] {
13+
break
14+
}
15+
}
16+
}
17+
18+
return dp[0]
19+
}
20+
}

0 commit comments

Comments
 (0)