Number of Matching Subsequences
Example :
Input:
S = "abcde"
words = ["a", "bb", "acd", "ace"]
Output:
3
Explanation:
There are three words in
words
that are a subsequence of
S
: "a", "acd", "ace".Last updated
Example :
Input:
S = "abcde"
words = ["a", "bb", "acd", "ace"]
Output:
3
Explanation:
There are three words in
words
that are a subsequence of
S
: "a", "acd", "ace".Last updated
class Solution:
def shortestWay(self, s: str, t: str) -> int:
ii = collections.defaultdict(list) #invertedIndex
for idx, v in enumerate(s):
ii[v].append(idx)
loopcnt = 1
curidx = -1
for tidx, tv in enumerate(t):
if tv not in ii:
return -1
iidx = bisect.bisect_left(ii[tv],curidx)
if iidx >= len(ii[tv]):
loopcnt += 1
curidx = ii[tv][0] + 1
else:
curidx = ii[tv][iidx] + 1
return loopcnt