forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0115-distinct-subsequences.kt
46 lines (38 loc) · 1.08 KB
/
0115-distinct-subsequences.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* DP bottom up
*/
class Solution {
fun numDistinct(s: String, t: String): Int {
val dp = Array(s.length + 1) { IntArray(t.length + 1) }
for (i in 0..s.length)
dp[i][t.length] = 1
for (i in s.length - 1 downTo 0) {
for (j in t.length - 1 downTo 0) {
if (s[i] == t[j])
dp[i][j] += (dp[i + 1][j + 1] + dp[i + 1][j])
else
dp[i][j] += dp[i + 1][j]
}
}
return dp[0][0]
}
}
/*
* DFS + Memoization
*/
class Solution {
fun numDistinct(s: String, t: String): Int {
val memo = Array(s.length) { IntArray(t.length) { -1 } }
fun dfs(i: Int, j: Int): Int {
if (j == t.length) return 1
if (i == s.length) return 0
if (memo[i][j] != -1) return memo[i][j]
if (s[i] == t[j])
memo[i][j] = dfs(i + 1, j + 1) + dfs(i + 1, j)
else
memo[i][j] = dfs(i + 1, j)
return memo[i][j]
}
return dfs(0, 0)
}
}