1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/检查单词是否为句中其他单词的前缀 [check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence].html
2022-03-29 12:43:11 +08:00

43 lines
1.9 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个字符串 <code>sentence</code> 作为句子并指定检索词为 <code>searchWord</code> ,其中句子由若干用 <strong>单个空格</strong> 分隔的单词组成。请你检查检索词 <code>searchWord</code> 是否为句子 <code>sentence</code> 中任意单词的前缀。</p>
<p>如果&nbsp;<code>searchWord</code> 是某一个单词的前缀,则返回句子&nbsp;<code>sentence</code> 中该单词所对应的下标(<strong>下标从 1 开始</strong>)。如果 <code>searchWord</code> 是多个单词的前缀,则返回匹配的第一个单词的下标(<strong>最小下标</strong>)。如果 <code>searchWord</code> 不是任何单词的前缀,则返回 <code>-1</code><strong> </strong></p>
<p>字符串 <code>s</code><strong>前缀</strong><code>s</code> 的任何前导连续子字符串。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>sentence = "i love eating burger", searchWord = "burg"
<strong>输出:</strong>4
<strong>解释:</strong>"burg" 是 "burger" 的前缀,而 "burger" 是句子中第 4 个单词。</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>sentence = "this problem is an easy problem", searchWord = "pro"
<strong>输出:</strong>2
<strong>解释:</strong>"pro" 是 "problem" 的前缀,而 "problem" 是句子中第 2 个也是第 6 个单词,但是应该返回最小下标 2 。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>sentence = "i am tired", searchWord = "you"
<strong>输出:</strong>-1
<strong>解释:</strong>"you" 不是句子中任何单词的前缀。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= sentence.length &lt;= 100</code></li>
<li><code>1 &lt;= searchWord.length &lt;= 10</code></li>
<li><code>sentence</code> 由小写英文字母和空格组成。</li>
<li><code>searchWord</code> 由小写英文字母组成。</li>
</ul>