给定字符串 s 和字符串数组 words, 返回 words[i] 中是s的子序列的单词个数 。
字符串的 子序列 是从原始字符串中生成的新字符串,可以从中删去一些字符(可以是none),
而不改变其余字符的相对顺序。
解题思路:
1 思路常规用二重循环即可,超时
2 定位每个元素起始遍历位置,超时
3 字典前缀树
4 python内置函数find()
class Solution(object):
def numMatchingSubseq(self, s, words):
res = 0
for word in words:
l = len(word)
cnt = 0
pos = -1
for char in word:
pos = s.find(char, pos+1)
if pos == -1:
break
else:
cnt += 1
if cnt == l:
res += 1
return res