The Indian Engineer

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

Posted on 6 mins

Two-Pointers String String Matching

Problem Statement

Link - Problem 1455

Question

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 burger", searchWord = "burg"
Output: 4
Explanation: "burg" is prefix of "burger" which is the 4th word in the sentence.

Example 2:

Input: sentence = "this problem is an easy problem", searchWord = "pro"
Output: 2
Explanation: "pro" is prefix of "problem" which is the 2nd and the 6th word in the sentence, but we return 2 as it's the minimal index.

Example 3:

Input: sentence = "i am tired", searchWord = "you"
Output: -1
Explanation: "you" is not a prefix of any word in the sentence.

Constraints:

Solution

class Solution {
public:
    int isPrefixOfWord(string sentence, string searchWord) {
        vector<string> words;
        string temp = "";

        for (char ch : sentence) {
            if (ch == ' ') {
                if (!temp.empty()) {
                    words.push_back(temp);
                    temp = "";
                }
            } else {
                temp += ch;
            }
        }
        if (!temp.empty()) {
            words.push_back(temp);
        }

        for (int i = 0; i < words.size(); i++) {
            if (words[i].substr(0, searchWord.size()) == searchWord) {
                return i + 1;
            }
        }

        return -1;
    }
};

Complexity Analysis

| Algorithm           | Time Complexity | Space Complexity |
| ------------------- | --------------- | ---------------- |
| String Tokenization | O(nm)      | O(n)             |

Explanation

Intial Thoughts

To solve this problem, we need to think of a way to break down the sentence into individual words. One approach could be to use the space as a delimiter to split the sentence into words. Another crucial thing to consider is how we will be comparing the searchWord with each word in the sentence. A prefix comparison will be necessary, and we need to think of a way to achieve that. Lastly, we need to consider the edge case where the searchWord is not a prefix of any word in the sentence.

Intuitive Analysis

Intuitively, we can solve this problem by breaking it down into three main steps. Firstly, we need to create a list of all words in the sentence, which can be achieved by splitting the sentence at each space. Secondly, we need to iterate over each word in the list and check if the searchWord is a prefix of that word. If it is, we need to return the index of that word. Lastly, we need to ensure that we return the index of the first word that the searchWord is a prefix of, which can be achieved by returning the index as soon as we find a match.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Easy difficulty.

Hints

First extract the words of the sentence.

Check for each word if searchWord occurs at index 0, if so return the index of this word (1-indexed)

If searchWord doesn’t exist as a prefix of any word return the default value (-1).


Similar Questions:

Title URL Difficulty
Counting Words With a Given Prefix https://leetcode.com/problems/counting-words-with-a-given-prefix Easy
Count Prefixes of a Given String https://leetcode.com/problems/count-prefixes-of-a-given-string Easy