java.lang.Object
g1401_1500.s1455_check_if_a_word_occurs_as_a_prefix_of_any_word_in_a_sentence.Solution

public class Solution extends Object
1455 - Check If a Word Occurs As a Prefix of Any Word in a Sentence.<p>Easy</p> <p>Given a <code>sentence</code> that consists of some words separated by a <strong>single space</strong> , and a <code>searchWord</code>, check if <code>searchWord</code> is a prefix of any word in <code>sentence</code>.</p> <p>Return <em>the index of the word in</em> <code>sentence</code> <em>( <strong>1-indexed</strong> ) where</em> <code>searchWord</code> <em>is a prefix of this word</em>. 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 <code>-1</code>.</p> <p>A <strong>prefix</strong> of a string <code>s</code> is any leading contiguous substring of <code>s</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> sentence = &ldquo;i love eating burger&rdquo;, searchWord = &ldquo;burg&rdquo;</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> &ldquo;burg&rdquo; is prefix of &ldquo;burger&rdquo; which is the 4th word in the sentence.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> sentence = &ldquo;this problem is an easy problem&rdquo;, searchWord = &ldquo;pro&rdquo;</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> &ldquo;pro&rdquo; is prefix of &ldquo;problem&rdquo; which is the 2nd and the 6th word in the sentence, but we return 2 as it&rsquo;s the minimal index.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> sentence = &ldquo;i am tired&rdquo;, searchWord = &ldquo;you&rdquo;</p> <p><strong>Output:</strong> -1</p> <p><strong>Explanation:</strong> &ldquo;you&rdquo; is not a prefix of any word in the sentence.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= sentence.length <= 100</code></li> <li><code>1 <= searchWord.length <= 10</code></li> <li><code>sentence</code> consists of lowercase English letters and spaces.</li> <li><code>searchWord</code> consists of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • isPrefixOfWord

      public int isPrefixOfWord(String sentence, String searchWord)