Skip to content

Commit 8788330

Browse files
committed
Solution Word break
1 parent 425262d commit 8788330

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

word-break/doitduri.swift

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
func wordBreak(_ s: String, _ wordDict: [String]) -> Bool {
3+
let wordSet = Set(wordDict)
4+
var dp = Array(repeating: false, count: s.count + 1)
5+
6+
dp[0] = true
7+
8+
for i in 1...s.count {
9+
for j in 0..<i {
10+
11+
guard dp[j] else { continue }
12+
13+
let startIndex = s.index(s.startIndex, offsetBy: j)
14+
let endIndex = s.index(s.startIndex, offsetBy: i)
15+
let word = String(s[startIndex..<endIndex])
16+
17+
if wordSet.contains(word) {
18+
dp[i] = true
19+
break
20+
}
21+
}
22+
}
23+
24+
return dp[s.count]
25+
}
26+
}

0 commit comments

Comments
 (0)