|
| 1 | +# [1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence](https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/) |
| 2 | + |
| 3 | + |
| 4 | +## 题目 |
| 5 | + |
| 6 | +Given a `sentence` that consists of some words separated by a **single space**, and a `searchWord`. |
| 7 | + |
| 8 | +You have to check if `searchWord` is a prefix of any word in `sentence`. |
| 9 | + |
| 10 | +Return *the index of the word* in `sentence` where `searchWord` is a prefix of this word (**1-indexed**). |
| 11 | + |
| 12 | +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**. |
| 13 | + |
| 14 | +A **prefix** of a string `S` is any leading contiguous substring of `S`. |
| 15 | + |
| 16 | +**Example 1**: |
| 17 | + |
| 18 | +``` |
| 19 | +Input: sentence = "i love eating burger", searchWord = "burg" |
| 20 | +Output: 4 |
| 21 | +Explanation: "burg" is prefix of "burger" which is the 4th word in the sentence. |
| 22 | +
|
| 23 | +``` |
| 24 | + |
| 25 | +**Example 2**: |
| 26 | + |
| 27 | +``` |
| 28 | +Input: sentence = "this problem is an easy problem", searchWord = "pro" |
| 29 | +Output: 2 |
| 30 | +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. |
| 31 | +
|
| 32 | +``` |
| 33 | + |
| 34 | +**Example 3**: |
| 35 | + |
| 36 | +``` |
| 37 | +Input: sentence = "i am tired", searchWord = "you" |
| 38 | +Output: -1 |
| 39 | +Explanation: "you" is not a prefix of any word in the sentence. |
| 40 | +
|
| 41 | +``` |
| 42 | + |
| 43 | +**Example 4**: |
| 44 | + |
| 45 | +``` |
| 46 | +Input: sentence = "i use triple pillow", searchWord = "pill" |
| 47 | +Output: 4 |
| 48 | +
|
| 49 | +``` |
| 50 | + |
| 51 | +**Example 5**: |
| 52 | + |
| 53 | +``` |
| 54 | +Input: sentence = "hello from the other side", searchWord = "they" |
| 55 | +Output: -1 |
| 56 | +
|
| 57 | +``` |
| 58 | + |
| 59 | +**Constraints**: |
| 60 | + |
| 61 | +- `1 <= sentence.length <= 100` |
| 62 | +- `1 <= searchWord.length <= 10` |
| 63 | +- `sentence` consists of lowercase English letters and spaces. |
| 64 | +- `searchWord` consists of lowercase English letters. |
| 65 | + |
| 66 | +## 题目大意 |
| 67 | + |
| 68 | +给你一个字符串 sentence 作为句子并指定检索词为 searchWord ,其中句子由若干用 单个空格 分隔的单词组成。请你检查检索词 searchWord 是否为句子 sentence 中任意单词的前缀。 |
| 69 | + |
| 70 | +- 如果 searchWord 是某一个单词的前缀,则返回句子 sentence 中该单词所对应的下标(下标从 1 开始)。 |
| 71 | +- 如果 searchWord 是多个单词的前缀,则返回匹配的第一个单词的下标(最小下标)。 |
| 72 | +- 如果 searchWord 不是任何单词的前缀,则返回 -1 。 |
| 73 | + |
| 74 | +字符串 S 的 「前缀」是 S 的任何前导连续子字符串。 |
| 75 | + |
| 76 | +## 解题思路 |
| 77 | + |
| 78 | +- 给出 2 个字符串,一个是匹配串,另外一个是句子。在句子里面查找带匹配串前缀的单词,并返回第一个匹配单词的下标。 |
| 79 | +- 简单题。按照题意,扫描一遍句子,一次匹配即可。 |
| 80 | + |
| 81 | +## 代码 |
| 82 | + |
| 83 | +```go |
| 84 | + |
| 85 | +package leetcode |
| 86 | + |
| 87 | +import "strings" |
| 88 | + |
| 89 | +func isPrefixOfWord(sentence string, searchWord string) int { |
| 90 | + for i, v := range strings.Split(sentence, " ") { |
| 91 | + if strings.HasPrefix(v, searchWord) { |
| 92 | + return i + 1 |
| 93 | + } |
| 94 | + } |
| 95 | + return -1 |
| 96 | +} |
| 97 | + |
| 98 | +``` |
0 commit comments