
You are given a string sentence containing words separated by single spaces, and a string searchWord.
Your task is to find the first word in the sentence that has searchWord as a prefix. Return the 1-indexed position of this word.
If searchWord is a prefix of multiple words, you must return the index of the first such word. If no word in the sentence has searchWord as a prefix, return -1.
The first line of input contains the sentence string.
The second line of input contains the searchWord string.
Print a single integer representing the 1-indexed position of the first word that has searchWord as a prefix.
If no such word is found, print -1.
A "prefix" is a contiguous sequence of characters at the beginning of a string. For example, "app" is a prefix of "apple".
i love eating burger
burg
4
The sentence is split into words: ["i", "love", "eating", "burger"].
- "burg" is not a prefix of "i", "love", or "eating".
- "burg" is a prefix of "burger", which is the 4th word.
i am tired
you
-1
The search word "you" is not a prefix of any of the words "i", "am", or "tired".
The expected time complexity is O(N*M), where N is the number of words and M is the length of the searchWord.
1 <= sentence.length <= 100
1 <= searchWord.length <= 10
Strings consist of lowercase English letters and spaces.
Time limit: 1 sec