Find the first word in a sentence that starts with a given search word

Quick Overview

Given a sentence of words separated by single spaces and a search word, return the 1-based position of the first word that starts with the search word, or -1 when no word does. It tests string splitting, prefix checks that ignore matches inside a word, and careful indexing.

Find the first word in a sentence that starts with a given search word

Company: Yelp

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a `sentence` that consists of some words separated by a single space, and a `search_word`, check if `search_word` is a prefix of any word in `sentence`. Return the index of the word in `sentence` (1-indexed) where `search_word` is a prefix of this word. If `search_word` is a prefix of more than one word, return the index of the first word (minimum index). If there is no such word, return `-1`. A prefix of a string `s` is any leading contiguous substring of `s`. ### Function Signature ```python def is_prefix_of_word(sentence: str, search_word: str) -> int: ``` ### Rules - Words are separated by exactly one space and are numbered from 1 in the order they appear. - A word matches when it starts with `search_word`, including when the word equals `search_word`. A search word longer than the word never matches. - An occurrence in the middle or at the end of a word does not count. - When several words match, return the smallest index. ### Constraints - `1 <= len(sentence) <= 100` - `1 <= len(search_word) <= 10` - `sentence` consists of lowercase English letters and spaces. - `search_word` consists of lowercase English letters. ### Examples ```text Input: sentence = "i love eating burger", search_word = "burg" Output: 4 ``` "burg" is a prefix of "burger", which is the 4th word in the sentence. None of the first three words starts with "burg". ```text Input: sentence = "this problem is an easy problem", search_word = "pro" Output: 2 ``` "pro" is a prefix of "problem", which is both the 2nd and the 6th word. The answer is 2 because it is the smaller index. ```text Input: sentence = "she sells seashells", search_word = "hell" Output: -1 ``` "hell" appears inside "seashells" but not at its start, and neither "she" nor "sells" starts with it, so no word matches.

Overview: Given a sentence of words separated by single spaces and a search word, return the 1-based position of the first word that starts with the search word, or -1 when no word does. It tests string splitting, prefix checks that ignore matches inside a word, and careful indexing.

Yelp logo
Yelp
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a sentence that consists of some words separated by a single space, and a search_word, check if search_word is a prefix of any word in sentence.

Return the index of the word in sentence (1-indexed) where search_word is a prefix of this word. If search_word is a prefix of more than one word, return the index of the first word (minimum index). If there is no such word, return -1.

A prefix of a string s is any leading contiguous substring of s.

Function Signature

def is_prefix_of_word(sentence: str, search_word: str) -> int:

Rules

  • Words are separated by exactly one space and are numbered from 1 in the order they appear.
  • A word matches when it starts with search_word , including when the word equals search_word . A search word longer than the word never matches.
  • An occurrence in the middle or at the end of a word does not count.
  • When several words match, return the smallest index.

Constraints

  • 1 <= len(sentence) <= 100
  • 1 <= len(search_word) <= 10
  • sentence consists of lowercase English letters and spaces.
  • search_word consists of lowercase English letters.

Examples

Input: sentence = "i love eating burger", search_word = "burg"
Output: 4

"burg" is a prefix of "burger", which is the 4th word in the sentence. None of the first three words starts with "burg".

Input: sentence = "this problem is an easy problem", search_word = "pro"
Output: 2

"pro" is a prefix of "problem", which is both the 2nd and the 6th word. The answer is 2 because it is the smaller index.

Input: sentence = "she sells seashells", search_word = "hell"
Output: -1

"hell" appears inside "seashells" but not at its start, and neither "she" nor "sells" starts with it, so no word matches.