Skip to content

Commit c7f0e2d

Browse files
authored
Update 1220-count-vowels-permutation.kt
1 parent 8d9ab23 commit c7f0e2d

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

kotlin/1220-count-vowels-permutation.kt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
1-
//DP
1+
//DP with O(1) space complexity
2+
class Solution {
3+
fun countVowelPermutation(n: Int): Int {
4+
val mod = 1_000_000_000 + 7
5+
var prev = LongArray (5) { 1L }
6+
7+
val a = 0
8+
val e = 1
9+
val i = 2
10+
val o = 3
11+
val u = 4
12+
13+
for (j in 1 until n) {
14+
val new = LongArray (5)
15+
new[a] = 0L + (prev[e] + prev[i] + prev[u]) % mod
16+
new[e] = 0L + (prev[a] + prev[i]) % mod
17+
new[i] = 0L + (prev[e] + prev[o]) % mod
18+
new[o] = 0L + (prev[i]) % mod
19+
new[u] = 0L + (prev[i] + prev[o]) % mod
20+
prev = new
21+
}
22+
23+
return (prev.sum()!! % mod).toInt()
24+
}
25+
}
26+
27+
//DP with O(n) space complexity
228
class Solution {
329
fun countVowelPermutation(n: Int): Int {
430
val mod = 1_000_000_000 + 7

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy