Skip to content

Commit 8a2b330

Browse files
committed
delight010.swift
1 parent ab5f639 commit 8a2b330

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

word-break/delight010.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 wordArray = Array(repeating: false, count: s.count + 1)
4+
wordArray[0] = true
5+
for i in 1...s.count {
6+
for j in 0..<i {
7+
if wordArray[j] {
8+
let startIndex = s.index(s.startIndex, offsetBy: j)
9+
let endIndex = s.index(s.startIndex, offsetBy: i)
10+
let word = String(s[startIndex..<endIndex])
11+
if wordDict.contains(word) {
12+
wordArray[i] = true
13+
}
14+
}
15+
}
16+
}
17+
return wordArray[s.count]
18+
}
19+
}
20+

0 commit comments

Comments
 (0)