Solution: Check If a Word is a Prefix of Any Word in a Sentence
Understand how to use a Trie data structure to efficiently check if a given search word is a prefix of any word in a sentence. Learn to split sentences, insert words into the Trie with their positions, and retrieve the earliest matching prefix index. This lesson helps you implement an optimized solution focusing on time and space efficiency.
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:
sentence.length...