Problem
Ask
Submissions

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

Medium
30 min
Explore how to identify whether a given search word appears as a prefix in any word within a sentence. Understand how to return the 1-based index of the first matching word or -1 if none match. This lesson helps you implement and optimize prefix searches using trie methods, enhancing your skills in handling string prefix problems effectively.

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.

A prefix of a string is any contiguous substring that begins at the first character.

Constraints:

  • 1<=1 <= sentence.length <=100<= 100

  • 1<=1 <= searchWord.length <=10<= 10

  • The sentence consists of lowercase English letters and spaces.

  • searchWord consists of lowercase English letters.

Problem
Ask
Submissions

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

Medium
30 min
Explore how to identify whether a given search word appears as a prefix in any word within a sentence. Understand how to return the 1-based index of the first matching word or -1 if none match. This lesson helps you implement and optimize prefix searches using trie methods, enhancing your skills in handling string prefix problems effectively.

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.

A prefix of a string is any contiguous substring that begins at the first character.

Constraints:

  • 1<=1 <= sentence.length <=100<= 100

  • 1<=1 <= searchWord.length <=10<= 10

  • The sentence consists of lowercase English letters and spaces.

  • searchWord consists of lowercase English letters.