|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../active-users "Active Users") |
| 9 | + |
| 10 | +[Next >](../maximum-number-of-vowels-in-a-substring-of-given-length "Maximum Number of Vowels in a Substring of Given Length") |
| 11 | + |
| 12 | +## [1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence (Easy)](https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence "检查单词是否为句中其他单词的前缀") |
| 13 | + |
| 14 | +<p>Given a <code>sentence</code> that consists of some words separated by a <strong>single space</strong>, and a <code>searchWord</code>.</p> |
| 15 | + |
| 16 | +<p>You have to check if <code>searchWord</code> is a prefix of any word in <code>sentence</code>.</p> |
| 17 | + |
| 18 | +<p>Return <em>the index of the word</em> in <code>sentence</code> where <code>searchWord</code> is a prefix of this word (<strong>1-indexed</strong>).</p> |
| 19 | + |
| 20 | +<p>If <code>searchWord</code> is a prefix of more than one word, return the index of the first word <strong>(minimum index)</strong>. If there is no such word return <strong>-1</strong>.</p> |
| 21 | + |
| 22 | +<p>A <strong>prefix</strong> of a string <code>S</code> is any leading contiguous substring of <code>S</code>.</p> |
| 23 | + |
| 24 | +<p> </p> |
| 25 | +<p><strong>Example 1:</strong></p> |
| 26 | + |
| 27 | +<pre> |
| 28 | +<strong>Input:</strong> sentence = "i love eating burger", searchWord = "burg" |
| 29 | +<strong>Output:</strong> 4 |
| 30 | +<strong>Explanation:</strong> "burg" is prefix of "burger" which is the 4th word in the sentence. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p><strong>Example 2:</strong></p> |
| 34 | + |
| 35 | +<pre> |
| 36 | +<strong>Input:</strong> sentence = "this problem is an easy problem", searchWord = "pro" |
| 37 | +<strong>Output:</strong> 2 |
| 38 | +<strong>Explanation:</strong> "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. |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p><strong>Example 3:</strong></p> |
| 42 | + |
| 43 | +<pre> |
| 44 | +<strong>Input:</strong> sentence = "i am tired", searchWord = "you" |
| 45 | +<strong>Output:</strong> -1 |
| 46 | +<strong>Explanation:</strong> "you" is not a prefix of any word in the sentence. |
| 47 | +</pre> |
| 48 | + |
| 49 | +<p><strong>Example 4:</strong></p> |
| 50 | + |
| 51 | +<pre> |
| 52 | +<strong>Input:</strong> sentence = "i use triple pillow", searchWord = "pill" |
| 53 | +<strong>Output:</strong> 4 |
| 54 | +</pre> |
| 55 | + |
| 56 | +<p><strong>Example 5:</strong></p> |
| 57 | + |
| 58 | +<pre> |
| 59 | +<strong>Input:</strong> sentence = "hello from the other side", searchWord = "they" |
| 60 | +<strong>Output:</strong> -1 |
| 61 | +</pre> |
| 62 | + |
| 63 | +<p> </p> |
| 64 | +<p><strong>Constraints:</strong></p> |
| 65 | + |
| 66 | +<ul> |
| 67 | + <li><code>1 <= sentence.length <= 100</code></li> |
| 68 | + <li><code>1 <= searchWord.length <= 10</code></li> |
| 69 | + <li><code>sentence</code> consists of lowercase English letters and spaces.</li> |
| 70 | + <li><code>searchWord</code> consists of lowercase English letters.</li> |
| 71 | +</ul> |
| 72 | + |
| 73 | +### Related Topics |
| 74 | + [[String](../../tag/string/README.md)] |
| 75 | + |
| 76 | +### Hints |
| 77 | +<details> |
| 78 | +<summary>Hint 1</summary> |
| 79 | +First extract the words of the sentence. |
| 80 | +</details> |
| 81 | + |
| 82 | +<details> |
| 83 | +<summary>Hint 2</summary> |
| 84 | +Check for each word if searchWord occurs at index 0, if so return the index of this word (1-indexed) |
| 85 | +</details> |
| 86 | + |
| 87 | +<details> |
| 88 | +<summary>Hint 3</summary> |
| 89 | +If searchWord doesn't exist as a prefix of any word return the default value (-1). |
| 90 | +</details> |
0 commit comments