Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. class Solution {
  2. func numDistinct(_ s: String, _ t: String) -> Int {
  3. if t == "" {
  4. return 1
  5. }
  6. if s == "" {
  7. return 0;
  8. }
  9. let s = Array(s)
  10. let t = Array(t)
  11. var dp = Array(repeating: Array(repeating: 0, count: t.count + 1), count: s.count + 1)
  12. for i in 0...s.count {
  13. dp[i][0] = 1
  14. }
  15. for i in 1...s.count {
  16. for j in 1...t.count {
  17. if s[i-1] == t[j-1] {
  18. dp[i][j] = dp[i-1][j-1] + dp[i-1][j]
  19. } else {
  20. dp[i][j] = dp[i-1][j]
  21. }
  22. }
  23. }
  24. return dp[s.count][t.count]
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement