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 <= 1001 <= searchWord.length <= 10Both consist of lowercase English letters and single spaces.
Hints for Check If a Word Occurs As a Prefix of Any Word in a Sentence
Hint 1
Split on spaces and test each word in order.
Hint 2
A prefix test compares only the first
searchWord.lengthcharacters.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
- Split
sentenceon single spaces. - For each word at 0-based index
i, compare its firstsearchWord.lengthcharacters withsearchWord. - Return
i + 1on the first match. - Return
-1if 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
searchWordcan 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 -1Java
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