Skip to content

Commit f703fa8

Browse files
committed
Add solution and test-cases for problem 1455
1 parent dbd4dda commit f703fa8

File tree

3 files changed

+86
-13
lines changed

3 files changed

+86
-13
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# [1455.Check If a Word Occurs As a Prefix of Any Word in a Sentence][title]
2+
3+
## Description
4+
Given a `sentence` that consists of some words separated by a **single space**, and a `searchWord`, check if `searchword` is a prefix of any word in `sentence`.
5+
6+
Return the index of the word in `sentence` (**1-indexed**) where `searchWord` is a prefix of this word. If `searchWord` is a prefix of more than one word, return the index of the first word (**minimum index**). If there is no such word return `-1`.
7+
8+
A **prefix** of a string `s` is any leading contiguous substring of `s`.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: sentence = "i love eating burger", searchWord = "burg"
14+
Output: 4
15+
Explanation: "burg" is prefix of "burger" which is the 4th word in the sentence.
16+
```
17+
18+
**Example 2:**
19+
20+
```
21+
Input: sentence = "this problem is an easy problem", searchWord = "pro"
22+
Output: 2
23+
Explanation: "pro" is prefix of "problem" which is the 2nd and the 6th word in the sentence, but we return 2 as it's the minimal index.
24+
```
25+
26+
**Example 3:**
27+
28+
```
29+
Input: sentence = "i am tired", searchWord = "you"
30+
Output: -1
31+
Explanation: "you" is not a prefix of any word in the sentence.
32+
```
33+
34+
## 结语
35+
36+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
37+
38+
[title]: https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence
39+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "strings"
4+
5+
type trie1455 struct {
6+
end int
7+
child [26]*trie1455
8+
}
9+
10+
func (tree *trie1455) insert1455(word string, index int) {
11+
cur := tree
12+
for _, b := range word {
13+
if cur.child[b-'a'] == nil {
14+
cur.child[b-'a'] = &trie1455{end: index, child: [26]*trie1455{}}
15+
}
16+
cur = cur.child[b-'a']
17+
}
18+
}
19+
20+
func (tree *trie1455) search1455(word string) int {
21+
cur := tree
22+
for _, b := range word {
23+
idx := b - 'a'
24+
if cur.child[idx] == nil {
25+
return -1
26+
}
27+
cur = cur.child[idx]
28+
}
29+
return cur.end
30+
}
31+
32+
func Solution(sentence string, searchWord string) int {
33+
tree := &trie1455{end: -1}
34+
for i, w := range strings.Split(sentence, " ") {
35+
tree.insert1455(w, i+1)
36+
}
37+
return tree.search1455(searchWord)
538
}

leetcode/1401-1500/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/Solution_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
inputs string
14+
searchWord string
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "i love eating burger", "burg", 4},
18+
{"TestCase2", "this problem is an easy problem", "pro", 2},
19+
{"TestCase3", "i am tired", "you", -1},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.searchWord)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.inputs, c.searchWord)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)