1078.Bigram分词

原题

用i,j,k三个指针一次遍历,每次各向前一位,当i和j分别与first和second匹配时,输出k指向 的字符

class Solution:
    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
        s = text.split(' ')
        n = len(s)
        res = []
        if n < 3:
            return res
        i, j, k = 0, 1, 2
        while k < n:
            if s[i] == first and s[j] == second:
                res.append(s[k])
            i += 1
            j += 1
            k += 1
        return res

最后更新于