2025-08-03 10:30:55 +0000 UTC

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

Code

class Solution:
    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
        for i, word in enumerate(sentence.split()):
            if word.startswith(searchWord):
                return i + 1
        return -1