diff --git a/leetcode/1401-1500/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/README.md b/leetcode/1401-1500/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/README.md new file mode 100644 index 000000000..cb178094b --- /dev/null +++ b/leetcode/1401-1500/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/README.md @@ -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 diff --git a/leetcode/1401-1500/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/Solution.go b/leetcode/1401-1500/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/Solution.go index d115ccf5e..349acc333 100755 --- a/leetcode/1401-1500/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/Solution.go +++ b/leetcode/1401-1500/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/Solution.go @@ -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) } diff --git a/leetcode/1401-1500/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/Solution_test.go b/leetcode/1401-1500/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/Solution_test.go index 14ff50eb4..757e6ae09 100755 --- a/leetcode/1401-1500/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/Solution_test.go +++ b/leetcode/1401-1500/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence/Solution_test.go @@ -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() { }