Search⌘ K
AI Features

Check If a Word is a Prefix of Any Word in a Sentence

Explore how to efficiently check if a given word is the prefix of any word within a sentence. This lesson helps you implement a solution that returns the 1-based index of the earliest word starting with the search word, applying trie concepts and string handling techniques to solve prefix matching problems.

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 searchWord is a prefix of multiple words, return the index of the earliest such word.

  • If no word starts with searchWord, return 1-1 ...