Difficulty: Easy | Acceptance: 68.80% | Paid: No Topics: Two Pointers, String, String Matching
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 there is no such word return -1.
A prefix of a string s is any leading contiguous substring of s.
- Examples
- Constraints
- Split and Compare
- Two Pointers
- Built-in String Methods
Examples
Input: sentence = "i love eating burger", searchWord = "burg"
Output: 4
Explanation: "burg" is prefix of "burger" which is the 4th word in the sentence.
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.
Since there is a 2, we return 2.
Input: sentence = "i am tired", searchWord = "you"
Output: -1
Explanation: There is no word in the sentence that has "you" as a prefix.
Constraints
1 <= sentence.length <= 100
1 <= searchWord.length <= 10
sentence consists of lowercase English letters and spaces.
searchWord consists of lowercase English letters.
Split and Compare
Intuition Split the sentence into individual words by spaces, then iterate through each word checking if the searchWord is a prefix using string comparison.
Steps
- Split the sentence by spaces to get an array of words
- Iterate through each word with its index
- Check if the word starts with searchWord
- Return the 1-indexed position if found, otherwise return -1
class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
words = sentence.split()
for i, word in enumerate(words):
if word.startswith(searchWord):
return i + 1
return -1Complexity
- Time: O(n × m) where n is the number of words and m is the length of searchWord
- Space: O(n) for storing the split words
- Notes: Simple and readable, but uses extra space for the array
Two Pointers
Intuition Use two pointers to traverse the sentence without splitting it into an array. Track word boundaries and compare characters directly with the searchWord.
Steps
- Initialize word counter and position pointers
- Skip spaces to find word boundaries
- For each word, compare characters with searchWord
- If all characters match, return the current word index
- Otherwise, skip to the next word
class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
word_index = 0
i = 0
n = len(sentence)
m = len(searchWord)
while i < n:
if sentence[i] == ' ':
i += 1
continue
word_index += 1
j = 0
while i < n and j < m and sentence[i] == searchWord[j]:
i += 1
j += 1
if j == m:
return word_index
while i < n and sentence[i] != ' ':
i += 1
return -1Complexity
- Time: O(n) where n is the length of sentence
- Space: O(1) constant extra space
- Notes: Optimal space efficiency, avoids creating an array
Built-in String Methods
Intuition Leverage language-specific built-in string methods for prefix checking, which are often optimized internally.
Steps
- Split the sentence into words
- Use built-in prefix checking methods (startsWith, indexOf, substring comparison)
- Return the first matching index or -1
class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
for i, word in enumerate(sentence.split(), 1):
if word[:len(searchWord)] == searchWord:
return i
return -1Complexity
- Time: O(n × m) where n is the number of words and m is the length of searchWord
- Space: O(n) for storing the split words
- Notes: Clean and idiomatic, uses optimized built-in methods