[编程题]3
  • 热度指数:394 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

给定英文句子S和字符串x,判断x是否为S中某些单词的前缀,若匹配到则输出第1个匹配单词的位置,否则输出-1

例如:输入"this is an easy problem.""eas",输出4

例如:输入"In love folly is always sweet""like",输出-1

例如:输入"Whatever is worth doing is worth doing well.""wor",输出3


示例1

输入

"this is an easy problem.","eas"

输出

4
发表于 2022-04-24 19:33:53 回复(0)
    int match_str_in_sentence(string s, string x) {
        // write code here
        int lens=s.size();
        int lenx=x.size();
        int a=0,res=1;
        while(a<lens-lenx){
            int count=0;
            if(s[a]==' ') res++;
            for(int i=0;i<lenx;i++){
                if(s[a+i]==x[i]) count++;
            }
            if(count==lenx) return res;
            a++;
        }
        return -1;
    }
发表于 2021-08-30 16:36:08 回复(0)
class Solution:
    def match_str_in_sentence(self , s , x ):
        # write code here
        str_arr = s.split()
        for i in range(0, len(str_arr)):
            if str_arr[i].startswith(x):
                return i + 1
        return -1

发表于 2021-08-22 15:15:16 回复(0)
[Python3] 分享一个普通方法
class Solution:
    def match_str_in_sentence(self , s , x ):
        s, lx = s.split(), len(x)
        for idx, word in enumerate(s):
            if len(word) >= lx and word[:lx] == x:
                return idx+1
        return -1
发表于 2021-07-26 17:27:51 回复(0)
class Solution:
    def match_str_in_sentence(self , s , x ):
        # write code here、
        strs = s.split()
        count = 0
        for s in strs:
            count += 1
            if len(s) < len(x):
                continue
            else:
                if s[:len(x)] == x:
                    return count
                else:
                    continue
        return -1

编辑于 2021-04-18 21:10:19 回复(0)
#
# 在句子中找到前缀是str的首个单词位置
# @param s string字符串 英文句子
# @param x string字符串 字符串
# @return int整型
class Solution:
    def match_str_in_sentence(self , s , x ):
        arr = s.split()
        for i in range(len(arr)):
            if x in arr[i][0:len(x)]:
                return i + 1
        return -1

发表于 2022-09-01 09:29:07 回复(0)
import java.util.*;
 
 
public class Solution {
    /**
     * 在句子中找到前缀是str的首个单词位置
     * @param s string字符串 英文句子
     * @param x string字符串 字符串
     * @return int整型
     */
    public int match_str_in_sentence (String s, String x) {
        // write code here
         
        if (s.indexOf(x) >= 1) {
            String[] str = s.split(" ");
            for (int i = 0; i < str.length; i++) {
                if(str[i].contains(x)){
                    return i+1;
                }
 
            }
        } else {
            return -1;
        }
 
      return 0;
    }
}

发表于 2021-11-13 00:24:30 回复(0)
    def match_str_in_sentence(self , s , x ):
        # write code here
        words = s.split(" ")
        l_x = len(x)
        res = -1
        for i, word in enumerate(words):
            # 判断是否为当前单词的前缀
            l_word = len(word)
            if l_word < l_x: continue
            if word[:l_x] == x:
                res = i+1
                break
        return res
发表于 2021-08-11 21:17:53 回复(0)
#
# 在句子中找到前缀是str的首个单词位置
# @param s string字符串 英文句子
# @param x string字符串 字符串
# @return int整型
#
class Solution:
    def match_str_in_sentence(self , s , x ):
        # write code here
        if s=="":return -1
        ss=s.split(' ')
        for i in range(len(ss)):
            if ss[i].find(x)!=-1:
                return (i+1)
        return -1

发表于 2021-06-02 19:28:04 回复(0)