Check If a Word Occurs As a Prefix of Any Word in a Sentence - Problem

Imagine you're building a smart autocomplete feature for a search engine! Given a sentence containing multiple words separated by spaces and a search term, you need to find if the search term appears as a prefix (beginning) of any word in the sentence.

Your task is to return the 1-indexed position of the first word that starts with the given search term. If no word starts with the search term, return -1.

Key Points:

  • A prefix means the search term appears at the very beginning of a word
  • Return the position of the first matching word (1-indexed, not 0-indexed)
  • If multiple words match, return the smallest index
  • Words are separated by single spaces

Example: In sentence "i love eating burger", searching for "burg" should return 4 because "burger" is the 4th word and starts with "burg".

Input & Output

example_1.py โ€” Basic Match
$ Input: sentence = "i love eating burger", searchWord = "burg"
โ€บ Output: 4
๐Ÿ’ก Note: The word "burger" is at position 4 (1-indexed) and starts with "burg", so we return 4.
example_2.py โ€” Multiple Matches
$ Input: sentence = "this problem is an easy problem", searchWord = "pro"
โ€บ Output: 2
๐Ÿ’ก Note: Both "problem" words start with "pro", but we return the index of the first occurrence, which is at position 2.
example_3.py โ€” No Match
$ Input: sentence = "i am tired", searchWord = "you"
โ€บ Output: -1
๐Ÿ’ก Note: None of the words "i", "am", or "tired" start with "you", so we return -1.

Visualization

Tap to expand
Library Book Search Analogy"AI Guide""Code Art""ML Book""Data Science""Python""Web Dev"๐Ÿ‘จโ€๐ŸซSearching for books starting with "Data"Process:1. Check "AI Guide" - doesn't start with "Data"2. Check "Code Art" - doesn't start with "Data"3. Check "ML Book" - doesn't start with "Data"4. Check "Data Science" - โœ“ MATCH! Return position 4Pos 1Pos 2Pos 3Pos 4 โœ“Pos 5Pos 6
Understanding the Visualization
1
Walk Along Shelf
Start from the leftmost book and move right, checking each book in order
2
Check Each Title
For each book, look at its title and see if it starts with 'Data'
3
First Match
When you find the first book starting with 'Data', note its position
4
Return Position
Tell the person the shelf position (1st book, 2nd book, etc.)
Key Takeaway
๐ŸŽฏ Key Insight: Just like finding books on a shelf, we check each word in order and return the position of the first match. The built-in `startsWith()` method is like having a quick way to check book titles without reading each letter manually!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n * m)

Where n is the number of words and m is the length of the search term. We check each word and compare up to m characters.

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space needed to store the array of words after splitting the sentence

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค sentence.length โ‰ค 100
  • 1 โ‰ค searchWord.length โ‰ค 10
  • sentence consists of lowercase English letters and spaces
  • searchWord consists of lowercase English letters
  • Words in sentence are separated by single spaces
Asked in
Amazon 15 Microsoft 12 Google 8 Meta 6
25.0K Views
Medium Frequency
~8 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen