Skip to content

Commit c04b594

Browse files
committed
Solve '139 Word Break'
1 parent 174b47c commit c04b594

File tree

24 files changed

+153
-3
lines changed

24 files changed

+153
-3
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.

139-word-break/spec.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 139. Word Break
2+
3+
<https://leetcode.com/problems/word-break/>
4+
5+
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.
6+
7+
Note:
8+
9+
* The same word in the dictionary may be reused multiple times in the segmentation.
10+
* You may assume the dictionary does not contain duplicate words.
11+
12+
Example 1:
13+
14+
```text
15+
Input: s = "leetcode", wordDict = ["leet", "code"]
16+
Output: true
17+
Explanation: Return true because "leetcode" can be segmented as "leet code".
18+
```
19+
20+
Example 2:
21+
22+
```text
23+
Input: s = "applepenapple", wordDict = ["apple", "pen"]
24+
Output: true
25+
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
26+
Note that you are allowed to reuse a dictionary word.
27+
```
28+
29+
Example 3:
30+
31+
```text
32+
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
33+
Output: false
34+
```

139-word-break/word.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package wordbreak
2+
3+
func wordBreak(s string, wordDict []string) bool {
4+
if s == "" || len(wordDict) == 0 {
5+
return false
6+
}
7+
8+
dict := make(map[string]bool, len(wordDict))
9+
10+
for _, w := range wordDict {
11+
dict[w] = true
12+
}
13+
14+
l := len(s)
15+
// remember indexes of next after breaks
16+
list := make(map[int]bool, l+1)
17+
list[0] = true
18+
19+
for i := 1; i <= l; i++ {
20+
for j := 0; j < i; j++ {
21+
cur := s[j:i]
22+
23+
if dict[cur] && list[j] {
24+
list[i] = true
25+
26+
break
27+
}
28+
}
29+
}
30+
31+
return list[l]
32+
}

139-word-break/word_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package wordbreak
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_wordBreak(t *testing.T) {
10+
type args struct {
11+
s string
12+
wordDict []string
13+
}
14+
15+
tests := []struct {
16+
name string
17+
args args
18+
want bool
19+
}{
20+
{
21+
name: `Return true because "leetcode" can be segmented as "leet code".`,
22+
args: args{
23+
s: "leetcode",
24+
wordDict: []string{"leet", "code"},
25+
},
26+
want: true,
27+
},
28+
{
29+
name: `Return true because "applepenapple" can be segmented as "apple pen apple".`,
30+
args: args{
31+
s: "applepenapple",
32+
wordDict: []string{"apple", "pen"},
33+
},
34+
want: true,
35+
},
36+
{
37+
name: `Return false cause could not segment`,
38+
args: args{
39+
s: "catsandog",
40+
wordDict: []string{"cats", "dog", "sand", "and", "cat"},
41+
},
42+
want: false,
43+
},
44+
{
45+
name: `Return true cause could be segmented, the same letter`,
46+
args: args{
47+
s: "aaaaaaa",
48+
wordDict: []string{"aaaa", "aaa"},
49+
},
50+
want: true,
51+
},
52+
{
53+
name: `Return false for empty word`,
54+
args: args{
55+
s: "",
56+
wordDict: []string{"aaaa", "aaa"},
57+
},
58+
want: false,
59+
},
60+
{
61+
name: `Return false for empty dict`,
62+
args: args{
63+
s: "aasad s",
64+
wordDict: nil,
65+
},
66+
want: false,
67+
},
68+
{
69+
name: `Return true for repeated`,
70+
args: args{
71+
s: "goalspecial",
72+
wordDict: []string{"go", "goal", "goals", "special"},
73+
},
74+
want: true,
75+
},
76+
}
77+
78+
for _, tt := range tests {
79+
t.Run(tt.name, func(t *testing.T) {
80+
got := wordBreak(tt.args.s, tt.args.wordDict)
81+
assert.Equal(t, tt.want, got)
82+
})
83+
}
84+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)