Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 1455 #1048

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# [1455.Check If a Word Occurs As a Prefix of Any Word in a Sentence][title]

## Description
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`.

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`.

A **prefix** of a string `s` is any leading contiguous substring of `s`.

**Example 1:**

```
Input: sentence = "i love eating burger", searchWord = "burg"
Output: 4
Explanation: "burg" is prefix of "burger" which is the 4th word in the sentence.
```

**Example 2:**

```
Input: sentence = "this problem is an easy problem", searchWord = "pro"
Output: 2
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.
```

**Example 3:**

```
Input: sentence = "i am tired", searchWord = "you"
Output: -1
Explanation: "you" is not a prefix of any word in the sentence.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
package Solution

func Solution(x bool) bool {
return x
import "strings"

type trie1455 struct {
end int
child [26]*trie1455
}

func (tree *trie1455) insert1455(word string, index int) {
cur := tree
for _, b := range word {
if cur.child[b-'a'] == nil {
cur.child[b-'a'] = &trie1455{end: index, child: [26]*trie1455{}}
}
cur = cur.child[b-'a']
}
}

func (tree *trie1455) search1455(word string) int {
cur := tree
for _, b := range word {
idx := b - 'a'
if cur.child[idx] == nil {
return -1
}
cur = cur.child[idx]
}
return cur.end
}

func Solution(sentence string, searchWord string) int {
tree := &trie1455{end: -1}
for i, w := range strings.Split(sentence, " ") {
tree.insert1455(w, i+1)
}
return tree.search1455(searchWord)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
inputs string
searchWord string
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "i love eating burger", "burg", 4},
{"TestCase2", "this problem is an easy problem", "pro", 2},
{"TestCase3", "i am tired", "you", -1},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.searchWord)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.inputs, c.searchWord)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading