示例:
输入: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
return 0 if len([a for a in str(sentence).split(" “) if a.find(word)==0])==0 else int([xx for xx in range(len([b for b in str(sentence).split(” “) ])) if [b for b in str(sentence).split(” “) ][xx]==[a for a in str(sentence).split(” ") if a.find(word)==0][0]][0])+1
def solution(sentence: str, word: str) -> int:
words = sentence.split(" ")
for i in words:
if word in i and word[0] == i[0]:
return words.index(i) + 1
else:
return -1
assert solution("i love hogwarts", "hog") == 3
assert solution("this problem is an easy problem", "pro") == 2
def solution(sentence: str, word: str) -> int:
# your code here
list_words = sentence.split(" ")
for i in range(len(list_words)):
if word == list_words[i][0:len(word)]:
return i + 1
return -1
assert solution("i love hogwarts", "hog") == 3
assert solution("i love hogwarts", "s") == -1
assert solution("this problem is an easy problem", "pro") == 2
def solution(sentence: str, word: str) -> int:
# your code here
list1 = sentence.split(" ")
for s in list1:
if s.startswith(word):
return list1.index(s)+1
return -1
def solution(sentence: str, word: str):
li=sentence.split(" ")
for i in range(len(li)):
if word==li[i][:3]:
return i+1
return -1
assert solution("i love hogwarts","hog") == 3
assert solution("this problem is an easy problem", "pro") == 2
def solution(sentence: str, word: str) -> int:
list1 = sentence.split()
for i in list1:
if word in i:
return list1.index(i)+1
assert solution("i love hogwarts", "hog") == 3
assert solution("this problem is an easy problem", "pro") == 2
def solution(sentence: str, word: str) -> int:
lis = sentence.split(' ')
for i in range(len(lis)):
if lis[i][:len(word)] == word:
return i+1
return -1
assert solution("i love hogwarts","hogwarts") == 3
assert solution("this problem is an easy problem", "pro") == 2
def get_min_index(sentence: str, word: str) -> int:
# your code here
str_list = sentence.split(" ")
resoult_int = -1
index = 1
for i in str_list:
if i[0:len(word)] in word:
resoult_int = index
break
index +=1
return resoult_int
def solution(sentence: str, word: str) -> int:
a=[i for i in sentence.split() if i[:len(word)] == word]
return sentence.split().index(a[0])+1 if len(a)>0 else -1
def solution(sentence: str, word: str) -> int:
#方法一
#result =[index for index,value in enumerate(sentence.split(' ')) if value.startswith(word)]
#return -1 if len(result) == 0 else result[0]+1
#方法二
for index, value in enumerate(sentence.split(' ')):
if value.startswith(word):
return index+1
return -1
def solution(sentence: str, word: str) -> int:
tmp_s=sentence.split(" ")
for i in range(len(tmp_s)):
if tmp_s[i].startswith(word):
return i+1
return -1
assert solution("i love hogwarts","hog") == 3
assert solution("this problem is an easy problem", "pro") == 2
def solution(sentence: str, word: str) -> int:
sentence_list=sentence.split(' ')
for i in range(1,len(sentence_list)+1):
for j in range(len(sentence_list[i-1])):
if word==sentence_list[i-1][:j]:
return i
return -1
assert solution("i love hogwarts", "hog") == 3
assert solution("this problem is an easy problem", "pro") == 2