Given a sentence that consists of some words separated by a single space, and a search_word, check if search_word is a prefix of any word in sentence.
Return the index of the word in sentence (1-indexed) where search_word is a prefix of this word. If search_word 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.
A prefix of a string s is any leading contiguous substring of s.
Function Signature
def is_prefix_of_word(sentence: str, search_word: str) -> int:
Rules
-
Words are separated by exactly one space and are numbered from 1 in the order they appear.
-
A word matches when it starts with
search_word
, including when the word equals
search_word
. A search word longer than the word never matches.
-
An occurrence in the middle or at the end of a word does not count.
-
When several words match, return the smallest index.
Constraints
-
1 <= len(sentence) <= 100
-
1 <= len(search_word) <= 10
-
sentence
consists of lowercase English letters and spaces.
-
search_word
consists of lowercase English letters.
Examples
Input: sentence = "i love eating burger", search_word = "burg"
Output: 4
"burg" is a prefix of "burger", which is the 4th word in the sentence. None of the first three words starts with "burg".
Input: sentence = "this problem is an easy problem", search_word = "pro"
Output: 2
"pro" is a prefix of "problem", which is both the 2nd and the 6th word. The answer is 2 because it is the smaller index.
Input: sentence = "she sells seashells", search_word = "hell"
Output: -1
"hell" appears inside "seashells" but not at its start, and neither "she" nor "sells" starts with it, so no word matches.