0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Leetcode #115: Distinct Subsequences

Posted at

First try failed at the test case
["fff", "ffff"] and ["avc", ""]

func numDistinct(_ s: String, _ t: String) -> Int {
    if t.count == 0 {
        return 1
    }
    if s.count < t.count {
        return 0
    }
    let S = Array(s)
    let T = Array(t)
    var tmp = Array(repeating: 1, count: S.count + 1)
    var dp = [Int]()
    for i in 1...T.count {
        dp = Array(repeating: 0, count: S.count + 1)
        for j in i...S.count {
            if S[j-1] == T[i-1] {
                dp[j] = dp[j-1] + tmp[j-1]
            } else {
                dp[j] = dp[j-1]
            }
        }
        tmp = dp
    }
    return dp[S.count]
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?