class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
words = sentence.split()
for idx, word in enumerate(words):
if word.startswith(searchWord):
return idx + 1
return -1
class Solution {
public:
int isPrefixOfWord(string sentence, string searchWord) {
istringstream iss(sentence);
string word;
int idx = 1;
while (iss >> word) {
if (word.find(searchWord) == 0) {
return idx;
}
idx++;
}
return -1;
}
};
class Solution {
public 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;
}
}
var isPrefixOfWord = function(sentence, searchWord) {
let words = sentence.split(" ");
for (let i = 0; i < words.length; i++) {
if (words[i].startsWith(searchWord)) {
return i + 1;
}
}
return -1;
};
Given a string sentence
consisting of words separated by single spaces, and a string searchWord
, determine if searchWord
is a prefix of any word in sentence
. Return the index (1-based) of the first word in sentence
where searchWord
is a prefix. If searchWord
is not a prefix of any word, return -1
.
sentence
is separated by a single space.searchWord
is a prefix.-1
.
To solve this problem, we need to check each word in the given sentence and see if it starts with the searchWord
. The most direct way is to split the sentence into its words, then check each word one by one.
A brute-force approach would be to loop through every word and check if the word starts with searchWord
. This is acceptable here, as the operations are efficient and the constraints are manageable.
Optimization is not strictly necessary, but we can make sure our solution is clean and leverages built-in string functions for checking prefixes, which are highly optimized.
The key insight is that we only care about the first word where searchWord
is a prefix, so we can return immediately when we find it.
sentence
into a list/array of words using the space character as a delimiter.searchWord
using a string prefix function (like startswith
in Python, startsWith
in Java/JavaScript, or find
in C++ with index 0).searchWord
, return its index (1-based).searchWord
, return -1
.This approach is efficient because it stops as soon as a match is found, and uses built-in string operations for clarity and speed.
Example:
sentence = "i love eating burger"
searchWord = "burg"
["i", "love", "eating", "burger"]
4
.
If searchWord = "xyz"
, none of the words start with "xyz", so we return -1
.
sentence
. Splitting and iterating through the words is linear in the size of the input.Since the problem size is small and the built-in string functions are fast, the straightforward approach is both simple and efficient.
The solution involves splitting the sentence into words and checking each word to see if it starts with the given searchWord
. We return the 1-based index of the first matching word, or -1 if there is no match. This is a classic string manipulation problem that is elegantly solved with built-in functions, and the approach is both readable and efficient.