美文网首页
792. 匹配子序列的单词数(jie)

792. 匹配子序列的单词数(jie)

作者: cptn3m0 | 来源:发表于2019-03-23 13:51 被阅读0次
    class Solution(object):
        def numMatchingSubseq(self, S: str, words: List[str]) -> int:
            res = 0
            d = {}
            for i in range(len(words)):
                if words[i][0] in d:
                    d[words[i][0]].append([i, 0])
                else:
                    d[words[i][0]] = [[i, 0]]
            for s in S:
                if s not in d:
                    continue
                cur = d[s]
                d[s] = []
                for [i, j] in cur:
                    if j == len(words[i]) - 1:
                        res += 1
                    else:
                        if words[i][j + 1] in d:
                            d[words[i][j + 1]].append([i, j + 1])
                        else:
                            d[words[i][j + 1]] = [[i, j + 1]]
            return res
    

    相关文章

      网友评论

          本文标题:792. 匹配子序列的单词数(jie)

          本文链接:https://www.haomeiwen.com/subject/hjhsvqtx.html