792. Number of Matching Subsequences

because this problem's bottle neck is the s length, 5*10^4, so how to optimized in this is the key.

Naive Solution - two poniters, TLE, because s is too big

time: O(n*m), , m is s length, n is words size(), m is so big

space: O(1)

移動雙指針去找是否 match

two pointer + hashset

這個解法可以過, 通過率還相當不錯...

應該是因為太多重複的字需要判斷, 所以把 pass 的字, not pass 的字都 memo 一下, 效率就差很多了

time: O(n*m), , m is s length, n is words size(), m is so big

space: O(n)

time: O(m + n*wlen*log(m) ), m is s length, n is words size(), wLen is each word len (in words)

state machine, next letter pointers

like this

time: O(26m + n*wLen), m is s length, n is words size(), wLen is each word len (in words)

space: O(26m)

1/4 version

Last updated

Was this helpful?