forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1220-count-vowels-permutation.kt
94 lines (78 loc) · 2.31 KB
/
1220-count-vowels-permutation.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//DP with O(1) space complexity
class Solution {
fun countVowelPermutation(n: Int): Int {
val mod = 1_000_000_000 + 7
var prev = LongArray (5) { 1L }
val a = 0
val e = 1
val i = 2
val o = 3
val u = 4
for (j in 1 until n) {
val new = LongArray (5)
new[a] = 0L + (prev[e] + prev[i] + prev[u]) % mod
new[e] = 0L + (prev[a] + prev[i]) % mod
new[i] = 0L + (prev[e] + prev[o]) % mod
new[o] = 0L + (prev[i]) % mod
new[u] = 0L + (prev[i] + prev[o]) % mod
prev = new
}
return (prev.sum()!! % mod).toInt()
}
}
//DP with O(n) space complexity
class Solution {
fun countVowelPermutation(n: Int): Int {
val mod = 1_000_000_000 + 7
val dp = Array (n + 1) { LongArray (5) }
for (j in 0 until 5)
dp[1][j] = 1
val a = 0
val e = 1
val i = 2
val o = 3
val u = 4
for (j in 2..n) {
dp[j][a] = 0L + (dp[j - 1][e] + dp[j - 1][i] + dp[j - 1][u]) % mod
dp[j][e] = 0L + (dp[j - 1][a] + dp[j - 1][i]) % mod
dp[j][i] = 0L + (dp[j - 1][e] + dp[j - 1][o]) % mod
dp[j][o] = 0L + (dp[j - 1][i]) % mod
dp[j][u] = 0L + (dp[j - 1][i] + dp[j - 1][o]) % mod
}
return (dp[n].sum()!! % mod).toInt()
}
}
//recursion + memoization
class Solution {
fun countVowelPermutation(n: Int): Int {
val mod = 1_000_000_000 + 7
val dp = Array (n) { IntArray (5) { -1 } }
val a = 0
val e = 1
val i = 2
val o = 3
val u = 4
val follow = mapOf(
a to listOf(e),
e to listOf(a, i),
i to listOf(a, e, o, u),
o to listOf(i, u),
u to listOf(a)
)
fun dfs(idx: Int, prev: Int): Int {
if (idx == n) return 1
if (dp[idx][prev] != -1) return dp[idx][prev]
var res = 0
follow[prev]?.forEach {
res = (res + dfs(idx + 1, it)) % mod
}
dp[idx][prev] = res
return res
}
var res = 0
follow.keys.forEach {
res = (res + dfs(1, it)) % mod
}
return res
}
}