Solution: Check If a Word is a Prefix of Any Word in a Sentence
Explore how to use a Trie to efficiently check if a given search word is a prefix of any word within a sentence. Understand the step-by-step process of splitting the sentence, inserting words into a Trie with their positions, and performing prefix checks to return the earliest matching word index or -1 if none found.
We'll cover the following...
Statement
You are given a sentence containing words separated by single spaces and a searchWord. Your task is to determine whether searchWord is a prefix of any word in the sentence.
Return the 1-based index of the first word in which searchWord appears as a prefix.
If
searchWordis a prefix of multiple words, return the index of the earliest such word.If no word starts with
searchWord, return.
A prefix of a string is any contiguous substring that begins at the first character.
Constraints:
...