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

Easy
517.

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

Check If a Word Occurs As a Prefix of Any Word in a Sentence is an easy coding problem on strings, asked in Amazon, Wipro and Yelp interviews.

A sentence is a list of words separated by single spaces, with no leading or trailing spaces.

Return the 1-based index of the first word in sentence that has searchWord as a prefix. If no word does, return -1.

Example 1

Input
sentence = "i love eating burger", searchWord = "burg"
Output
4
Explanation
"burger" is the 4th word and starts with "burg".

Example 2

Input
sentence = "this problem is an easy problem", searchWord = "pro"
Output
2
Explanation
The first match wins, even though the 6th word also matches.

Example 3

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

Constraints

  • 1 <= sentence.length <= 100
  • 1 <= searchWord.length <= 10
  • Both consist of lowercase English letters and single spaces.
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} sentence
 * @param {string} searchWord
 * @return {number}
 */
var isPrefixOfWord = function(sentence, searchWord) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

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

Check If a Word Occurs As a Prefix of Any Word in a Sentence solution: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript.

Check If a Word Occurs As a Prefix of Any Word in a Sentence is an easy coding problem on strings, asked in Amazon, Wipro and Yelp interviews.

  • Difficulty: Easy
  • Topics: Strings
  • Asked at: Amazon, Wipro, Yelp
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

A sentence is a list of words separated by single spaces, with no leading or trailing spaces.

Return the 1-based index of the first word in sentence that has searchWord as a prefix. If no word does, return -1.

Example 1

Input: sentence = "i love eating burger", searchWord = "burg"
Output: 4
Explanation: "burger" is the 4th word and starts with "burg".

Example 2

Input: sentence = "this problem is an easy problem", searchWord = "pro"
Output: 2
Explanation: The first match wins, even though the 6th word also matches.

Example 3

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

Constraints

  • 1 <= sentence.length <= 100
  • 1 <= searchWord.length <= 10
  • Both consist of lowercase English letters and single spaces.

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

  1. Hint 1

    Split on spaces and test each word in order.

  2. Hint 2

    A prefix test compares only the first searchWord.length characters.

  3. Hint 3

    Return as soon as you find a match — indices are 1-based.

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

Split the sentence into words and run a prefix test on each, left to right, stopping at the first hit.

Approach

  1. Split sentence on single spaces.
  2. For each word at 0-based index i, compare its first searchWord.length characters with searchWord.
  3. Return i + 1 on the first match.
  4. Return -1 if the loop finishes.

Why it works

The problem asks for the earliest match, and scanning in order with an early return delivers exactly that.

Complexity

  • Time — O(n) over the sentence length
  • Space — O(n) for the split

Pitfalls

  • Returning the 0-based index is the most common mistake — the answer is 1-based.
  • Using a substring search instead of a prefix test matches "pro" inside "approve", which is wrong.
  • A word shorter than searchWord can never match; slicing must not go out of bounds.

Reference solution

Python

def isPrefixOfWord(sentence: str, searchWord: str) -> int:
    for i, w in enumerate(sentence.split(" ")):
        if w.startswith(searchWord):
            return i + 1
    return -1

Java

public static int isPrefixOfWord(String sentence, String searchWord) {
    String[] words = sentence.split(" ");
    for (int i = 0; i < words.length; i++) {
        if (words[i].startsWith(searchWord)) return i + 1;
    }
    return -1;
}

C++

int isPrefixOfWord(string sentence, string searchWord) {
    vector<string> words;
    string cur;
    for (char c : sentence) {
        if (c == ' ') { words.push_back(cur); cur.clear(); }
        else cur += c;
    }
    words.push_back(cur);
    for (int i = 0; i < (int) words.size(); i++) {
        if (words[i].size() >= searchWord.size() && words[i].compare(0, searchWord.size(), searchWord) == 0) return i + 1;
    }
    return -1;
}

JavaScript

var isPrefixOfWord = function(sentence, searchWord) {
    const words = sentence.split(" ");
    for (let i = 0; i < words.length; i++) {
        if (words[i].slice(0, searchWord.length) === searchWord) return i + 1;
    }
    return -1;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings