Skip to content

Practice Check If a Word Occurs As a Prefix of Any Word in a Sentence

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#1455
LeetCode Problem

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

Problem Statement

Given a sentence that consists of some words separated by a single space, and a searchWord, check if searchWord is a prefix of any word in sentence. Return the index of the word in sentence (1-indexed) where searchWord is a prefix of this word. If searchWord 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.

Source: Check If a Word Occurs As a Prefix of Any Word in a Sentence on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
sentence = "i love eating burger", searchWord = "burg"
Output
4
Explanation

The words in order are "i", "love", "eating" and "burger". The first three do not begin with "burg"; the fourth does, so return 4. The result is a one-based word position, not a character position or a zero-based array index. A matching substring later inside a word would not count as a prefix.

# Example 2

Input
sentence = "this problem is an easy problem", searchWord = "pro"
Output
2
Explanation

"problem" begins with "pro" at word positions 2 and 6. The task asks for the first matching word, so return 2 and stop searching. Continuing to overwrite the answer after later matches would incorrectly return 6. The separate word "is" does not affect the earlier valid prefix match.

# Example 3

Input
sentence = "i am tired", searchWord = "you"
Output
-1
Explanation

The words "i", "am" and "tired" do not begin with "you". After checking every word without a match, return -1. The search compares characters at the start of each whole word and does not join neighboring words to manufacture a prefix match.

Constraints

1 <= sentence.length <= 100
1 <= searchWord.length <= 10
sentence consists of lowercase English letters and spaces.
searchWord consists of lowercase English letters.

How to solve Check If a Word Occurs As a Prefix of Any Word in a Sentence

Original notes for interview preparation, written for this problem. Reviewed .

Key insight

A word begins at index 0 or immediately after a space, so you only need to compare searchWord against the characters starting at each of those positions; the first position that matches in full gives the one-based answer.

Why brute force is not enough

Generating every prefix of every word and comparing each against searchWord, or scanning for searchWord as a substring and then checking whether each hit sits on a word boundary, does redundant work and invites boundary bugs. With sentence.length at most 100 none of this is slow, so the interviewer is judging precision: prefix versus substring, one-based indexing, and first match wins.

Approach

  1. Split the sentence on single spaces, or equivalently scan it and treat index 0 and every index following a space as the start of a word; keep a running word counter that starts at 1.
  2. At each word start, first confirm the word has at least as many characters as searchWord; a shorter word cannot have it as a prefix.
  3. Compare searchWord character by character against the word from its first character; stop at the first mismatch and move on to the next word.
  4. On a complete match return the current word counter immediately, which guarantees the minimum index.
  5. If the scan finishes without a match, return -1.

Walkthrough of Example 1

sentence = "i love eating burger", searchWord = "burg" → 4

  1. Word 1 is i; it is shorter than burg, so skip it.
  2. Word 2 is love; l differs from b, skip it.
  3. Word 3 is eating; e differs from b, skip it.
  4. Word 4 is burger; b, u, r, g match the four characters of burg, so return 4.

Time and space

Time O(L) · Space O(1). L is the sentence length; each character is visited a bounded number of times because searchWord has at most 10 characters, and the pointer version allocates nothing beyond a few counters (splitting into a word list would make space O(L)).

Interview pitfalls

  • Returning a zero-based index or a character offset; the problem wants the one-based position of the word.
  • Treating a match inside a word, such as burg inside hamburger, as a prefix; matches must begin at a word boundary.
  • Calling a startsWith routine on the whole sentence instead of on each word, which only ever checks the first word.
  • Overwriting the answer on later matches instead of stopping at the first, which turns the expected 2 in example 2 into 6.
  • Indexing past the end of a short word when comparing characters without the length check.

Follow-up questions interviewers ask

  • If you have thousands of searchWord queries against the same sentence, how would a trie of the words reduce each query to time proportional to the length of searchWord?
  • How does the solution change if words may be separated by multiple spaces or punctuation?
  • What if you must return all matching word indices instead of the first?

Related LeetCode Problems

Practice more problems similar to Check If a Word Occurs As a Prefix of Any Word in a Sentence.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.