Check If a Word Occurs As a Prefix of Any Word in a Sentence
Given a `sentence` of words separated by single spaces and a string `searchWord`, check whether `searchWord` is a prefix of any word in `sentence`. Return the 1-indexed position of the first such word, or `-1` if there is none. A prefix of a string is any leading contiguous substring of it.
30:00
Check If a Word Occurs As a Prefix of Any Word in a Sentence
Given a sentence of words separated by single spaces and a string searchWord, check whether searchWord is a prefix of any word in sentence. Return the 1-indexed position of the first such word, or -1 if there is none. A prefix of a string is any leading contiguous substring of it.
Example 1:
Input: {"sentence":"i love eating burger","searchWord":"burg"}
Output: 4
Constraints:
1≤sentence.length≤100
1≤searchWord.length≤10
sentence consists of lowercase English letters and spaces.
searchWord consists of lowercase English letters.
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"sentence":"i love eating burger","searchWord":"burg"}
word = 1 counts which word we are in, and j = 0 points at the next letter of "burg" that the current word must match.