#94 ๐ 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence ๐๐ง
Given a sentence and a search word, determine if the search word appears as a prefix in any word of the sentence. If it does, return the 1-indexed position of the first word that matches. Otherwise, return -1
.
๐ง Problem Statement
We are tasked with finding if a given searchWord
is a prefix of any word in a sentence
. If it is, we return the 1-indexed position of the first word that matches. If no word matches, return -1
.
A prefix is defined as a leading contiguous substring of a string. For example, โburgโ is a prefix of โburgerโ because โburgerโ starts with โburgโ.
๐ Input and Output:
Input:
sentence
(string): A sentence containing words separated by single spaces.searchWord
(string): A word we want to check as a prefix.
Output:
- Return the 1-indexed position of the word where
searchWord
is a prefix. - If
searchWord
is not a prefix of any word, return-1
.
โจ Examples
Example 1:
Input:
sentence = "i love eating burger"
searchWord = "burg"
Output:
4
Explanation:
The word โburgerโ is the 4th word in the sentence, and โburgโ is a prefix of it.
Example 2:
Input:
sentence = "this problem is an easy problem"
searchWord = "pro"
Output:
2
Explanation:
The word โproblemโ appears twice in the sentence (at positions 2 and 6). We return the minimal index, which is 2
.
Example 3:
Input:
sentence = "i am tired"
searchWord = "you"
Output:
-1
Explanation:
The word โyouโ is not a prefix of any word in the sentence, so the result is -1
.
๐ก Edge Cases
- Case with no match:
- Input:
sentence = "hello world"
,searchWord = "x"
. - Output:
-1
. - Explanation: None of the words in the sentence start with โxโ.
- Input:
- Case with empty sentence:
- Input:
sentence = ""
,searchWord = "test"
. - Output:
-1
. - Explanation: An empty sentence has no words to check.
- Input:
- Case where all words match:
- Input:
sentence = "abc abc abc"
,searchWord = "a"
. - Output:
1
. - Explanation: All words start with โaโ, but the first occurrence is at position
1
.
- Input:
- Case where searchWord equals the full word:
- Input:
sentence = "apple banana", searchWord = "apple"
. - Output:
1
. - Explanation: The
searchWord
matches the first word entirely, so it is still a prefix.
- Input:
๐ ๏ธ Solution
Approach
-
Split the Sentence:
Break thesentence
into words using thesplit()
function. -
Check for Prefix:
Iterate through the list of words, checking if thesearchWord
is a prefix of any word using Pythonโsstartswith()
method. -
Return the Index:
If a match is found, return the 1-indexed position (index + 1). If no match is found, return-1
.
๐น Python Code (Single Solution)
class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
# Split the sentence into words
words = sentence.split()
# Iterate through words and check for the prefix
for i, word in enumerate(words):
if word.startswith(searchWord):
return i + 1 # 1-indexed position
# If no match, return -1
return -1
Time Complexity
- Splitting the sentence:
O(N)
whereN
is the length of thesentence
. - Iterating through words:
O(W)
whereW
is the number of words in thesentence
. - Checking prefix for each word:
O(L)
whereL
is the length of the longest word.
Thus, the overall time complexity is O(N + W * L)
.
Space Complexity
- Splitting the sentence creates a list of words, so the space complexity is
O(W)
, whereW
is the number of words.
๐ Example Walkthrough
Example Input:
sentence = "coding is fun and fantastic"
searchWord = "fan"
Step-by-Step Execution:
- Split the sentence:
words = ["coding", "is", "fun", "and", "fantastic"]
- Iterate through words:
i = 0
,word = "coding"
โ"fan"
is not a prefix.i = 1
,word = "is"
โ"fan"
is not a prefix.i = 2
,word = "fun"
โ"fan"
is not a prefix.i = 3
,word = "and"
โ"fan"
is not a prefix.i = 4
,word = "fantastic"
โ"fan"
is a prefix!
- Return Index:
The word โfantasticโ is at position5
(1-indexed).
Output:
5
โ๏ธ Conclusion
This problem is straightforward with the use of string operations like startswith()
. By splitting the sentence into words and checking each for a prefix match, we efficiently find the result.
Key Takeaways:
- Use Pythonโs built-in string manipulation functions for clean and concise solutions.
- Remember to account for edge cases like no matches, an empty sentence, or multiple matches.
With this approach, you can solve the problem in optimal time while maintaining clarity in your code. ๐