-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution and test-cases for problem 1455
- Loading branch information
Showing
3 changed files
with
86 additions
and
13 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
...500/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
37 changes: 35 additions & 2 deletions
37
...e/1401-1500/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters