给你一个字符串句子
sentence
和检索词word
,其中句子由若干空格分隔的单词组成。请编写一个函数,判断检索词word
是否是句子中任意单词的前缀。
如果是句子中某个单词的前缀,则返回该单词出现的下标(从1开始);如果存在匹配多个,则取第一个单词的下标;如果不存在,则返回-1
。
示例:
输入:sentence = “i love hogwarts”, word = “hog”
输出:3
解释:"hog"是单词"hogwarts"的前缀,而"hogwarts"是第3个单词。
题目难度:简单
题目来源:力扣 1455
def solution(sentence: str, word: str) -> int:
# your code here
assert solution("i love hogwarts") == 3
assert solution("this problem is an easy problem", "pro") == 2