Advertisement

Check If a Word Occurs As a Prefix of Any Word in a Sentence - LeetCode 1455 Solution

Check If a Word Occurs As a Prefix of Any Word in a Sentence - Complete Solution Guide

Check If a Word Occurs As a Prefix of Any Word in a Sentence is LeetCode problem 1455, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 . Example 1: Input: sentence = "i love eating

Detailed Explanation

The problem asks you to find the first word in a sentence that starts with a given `searchWord`. The input is a sentence (string) and the `searchWord` (string). The output is the 1-indexed position of the word in the sentence if found; otherwise, -1. The sentence consists of words separated by single spaces. The `searchWord` must be a prefix (beginning substring) of a word, not necessarily the entire word itself. Constraints limit the length of the sentence and `searchWord` to relatively small values.

Solution Approach

The provided solutions all follow a similar approach: they first split the sentence into individual words using the space character as a delimiter. Then, they iterate through the words, checking if each word starts with the given `searchWord`. The index of the first word that satisfies this condition is returned (plus 1 for 1-based indexing). If no word matches, -1 is returned.

Step-by-Step Algorithm

  1. Step 1: Split the input sentence string into an array of individual words using the space character (' ') as the delimiter.
  2. Step 2: Iterate through the array of words using a loop (e.g., `for` loop).
  3. Step 3: For each word, check if it starts with the `searchWord` using a `startsWith()` function or string comparison (e.g., `strncmp` in C).
  4. Step 4: If a word starts with `searchWord`, return its index (plus 1 for 1-based indexing).
  5. Step 5: If the loop completes without finding a matching word, return -1.

Key Insights

  • Insight 1: The problem can be efficiently solved by iterating through the words of the sentence and checking if each word starts with the `searchWord` using the `startsWith()` method (or equivalent).
  • Insight 2: String splitting is a fundamental operation for processing text. Efficient string splitting is crucial for performance.
  • Insight 3: Handling edge cases like an empty sentence or `searchWord` and a sentence without words matching the prefix are essential to prevent unexpected behavior or errors.

Complexity Analysis

Time Complexity: O(n*m)

Space Complexity: O(n)

Topics

This problem involves: Two Pointers, String, String Matching.

Companies

Asked at: Yelp.