lc0320

✅ Bigram 分词

描述

给出第一个词 first 和第二个词 second,考虑在某些文本 text 中可能以 "first second third" 形式出现的情况,其中 second 紧随 first 出现,third 紧随 second 出现。

对于每种这样的情况,将第三个词 "third" 添加到答案中,并返回答案。

 示例 1:

输入:text = "alice is a good girl she is a good student", first = "a", second = "good"
输出:["girl","student"]
示例 2:

输入:text = "we will we will rock you", first = "we", second = "will"
输出:["we","rock"]

解答

也就是找到两个重复出现的单词呗,然后输出它们两个各自后面跟着的一个。

哦哦, 原来 first and second 是已经给出的,不需要我们去自己找。

开放性问题:你会自己找到 字符串中 可以当作 first second 的单词吗

c

char ** findOcurrences(char * text, char * first, char * second, int* returnSize){
    char** ans=(char**)malloc(1000*sizeof(char*));
    char** res=(char*)malloc(1000*sizeof(char*));
    int len=strlen(text);
    int left=0,right=0;
    int num=0;
    //tt 这整个for loop就是py 的一句: text.split()
    //tt 用空格,分割句子,为单独的一个个 单词
    for(int i=0;i<len;i++)
    {
        if(text[i]==' ')
        {
            right=i-1;
            res[num]=(char*)malloc(right-left+2);
            strncpy(res[num],text+left,right-left+1);
            res[num][right-left+1]='\0';
            num++;
            left=i+1;
        }
        if(i==len-1)
        {
            right=i;
            res[num]=(char*)malloc(right-left+2);
            strncpy(res[num],text+left,right-left+1);
            res[num][right-left+1]='\0';
            num++;
        }
    }
    int count=0;

    //tt 这个for 就是我的py的:ret.append(theOneShouldBeAppend)
    for(int i=0;i<num-2;i++)
    {
        if(!strcmp(res[i],first)&&!strcmp(res[i+1],second))
        {
            len=strlen(res[i+2]);
            ans[count]=(char*)malloc(len+1);
            strcpy(ans[count],res[i+2]);
            ans[count][len]='\0';
            count++;
        }
    }
    * returnSize=count;
    return ans;
}

py

class Solution:
    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
        arr = text.split()
        ret = []
        for i in range(len(arr) - 2):
            if (arr[i] == first) and (arr[i+1] == second):
                ret.append(arr[i+2])
        return ret
'''
执行用时 :
40 ms
, 在所有 Python3 提交中击败了
34.94%
的用户
内存消耗 :
13.6 MB
, 在所有 Python3 提交中击败了
5.06%
的用户
'''

猜你喜欢

转载自www.cnblogs.com/paulkg12/p/12533819.html