【每日一题20220309】单词前缀

:woman_mage:给你一个字符串句子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
1 个赞
def solution(sentence: str, word: str) -> int:
    # 0309 单词前缀
    sen_list = sentence.split(" ")
    for sen in sen_list:
        if sen.startswith(word):
            return sen_list.index(sen)+1
    return -1

assert solution("i love hogwarts", 'hog') == 3
assert solution("this problem is an easy problem", "pro") == 2

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
public int isPrefixOfWord(String sentence, String searchWord) {
        // 题解一
        // String[] strArray = sentence.split(" ");
        // for(int i=0;i<strArray.length;i++){
        //     if(strArray[i].startsWith(searchWord))
        //         return i+1;
        // }
        // return -1;
        
        // 题解二
        // 记录第几个单词 
        int index = 1;
        // 记录searchWord的第几个字母
        int indexSearch = 0,len = searchWord.length()-1;
        // 记录是否以searchWord开头
        boolean isStart = true;
        for(int i=0;i<sentence.length();i++){
            if(sentence.charAt(i) == (' ')){
                index++;
                indexSearch = 0;
                isStart = true;
            }else{
                if(indexSearch <= len){
                    if(sentence.charAt(i) != searchWord.charAt(indexSearch)){
                        // 只要有不一样的就置为false
                        isStart = false;
                    }
                    if(indexSearch++ == len && isStart)
                        return index;
                }
            }
        }
        return -1;
    }
    def solution(sentence: str, word: str) -> int:
        for k,v in enumerate(sentence.split(' ')):
            if v.startswith(word):
                return k + 1
        return -1
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
1 个赞

1652423379(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