#1455
Check If a Word Occurs As a Prefix of Any Word in a Sentence
newbie · 295 · lc easy +20 · verified · 68.8% accepted · 1,325 likes · top 76%
Description
You are given a space-separated sentence and a searchWord. Scan the words of sentence from left to right using 1-based indexing and return the position of the first word that begins with searchWord as a prefix. If no such word exists, return -1.
A prefix of a string is any contiguous leading portion of it.
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.
Code
1
2
3