-
Notifications
You must be signed in to change notification settings - Fork 6
/
WordBreak.swift
47 lines (36 loc) · 1.3 KB
/
WordBreak.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
// WordBreak.swift
// LeetCode.swift
//
// Created by 叶帆 on 2017/10/15.
// Copyright © 2017年 Suzhou Coryphaei Information&Technology Co., Ltd. All rights reserved.
//
/**
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.
For example, given
s = "leetcode",
dict = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code".
*/
import Foundation
class Solution {
func wordBreak(_ s: String, _ wordDict: [String]) -> Bool {
if s.isEmpty {
return true
}
if wordDict.count == 0 {
return false
}
var wordArray = Array.init(repeating: false, count: s.count + 1)
wordArray[0] = true
for i in 1...s.count {
for j in stride(from: i-1, through: 0, by: -1) {
if wordArray[j] && wordDict.contains(String(s[s.index(s.startIndex, offsetBy: j)..<s.index(s.startIndex, offsetBy: i)])) {
wordArray[i] = true
break
}
}
}
return wordArray[s.count]
}
}