-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
1220-count-vowels-permutation.java
141 lines (117 loc) · 3.93 KB
/
1220-count-vowels-permutation.java
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
class Solution {
int MOD = (int) 1e9+7;
public int countVowelPermutation(int n) {
if (n == 1) {
return 5;
}
long[][] dp = new long[n + 1][5];
for (int j = 0; j < 5; j++) {
dp[1][j] = 1;
}
for (int i = 2; i <= n; i++) {
dp[i][0] = dp[i - 1][1];
dp[i][1] = (dp[i - 1][0] + dp[i - 1][2]) % MOD;
dp[i][2] = (dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][3] + dp[i - 1][4]) % MOD;
dp[i][3] = (dp[i - 1][2] + dp[i - 1][4]) % MOD;
dp[i][4] = dp[i - 1][0];
}
long result = 0;
for (int j = 0; j < 5; j++) {
result = (result + dp[n][j]) % MOD;
}
return (int)result;
}
}
/* Top Down Approach
-----------------------------------------
Time Complexity : O(n)
Space Complexity : O(n)
----------------------------------------*/
class Solution {
HashMap<String, Integer> memo = new HashMap<>();
int MOD = (int) 1e9+7;
public int countVowelPermutation(int n) {
long[] counts = getBaseCounts();
if(n == 1) {
return getSum(counts);
}
Map<Integer, List<Integer>> mapNextCounting;
mapNextCounting = getNextCountMapping();
for(int i=1; i<n; i++) {
counts = getNextCounts(counts, mapNextCounting);
}
return getSum(counts);
}
}
/* Bottom-Up Approach
-----------------------------------------
Time Complexity : O(n)
Space Complexity : O(n)
----------------------------------------*/
class Solution {
int MOD = (int) 1e9 + 7;
public int countVowelPermutation(int n) {
if (n == 1) {
return 5;
}
long[][] dp = new long[n + 1][5];
for (int j = 0; j < 5; j++) {
dp[1][j] = 1;
}
for (int i = 2; i <= n; i++) {
dp[i][0] = dp[i - 1][1];
dp[i][1] = (dp[i - 1][0] + dp[i - 1][2]) % MOD;
dp[i][2] = (dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][3] + dp[i - 1][4]) % MOD;
dp[i][3] = (dp[i - 1][2] + dp[i - 1][4]) % MOD;
dp[i][4] = dp[i - 1][0];
}
long result = 0;
for (int j = 0; j < 5; j++) {
result = (result + dp[n][j]) % MOD;
}
return (int)result;
}
}
/* Top Down Approach
-----------------------------------------
Time Complexity : O(n)
Space Complexity : O(n)
----------------------------------------*/
class Solution {
HashMap<String, Integer> memo = new HashMap<>();
int MOD = (int) 1e9 + 7;
public int countVowelPermutation(int n) {
long ans = 0;
ans = (ans + dfs('a', n, 1)) % MOD;
ans = (ans + dfs('e', n, 1)) % MOD;
ans = (ans + dfs('i', n, 1)) % MOD;
ans = (ans + dfs('o', n, 1)) % MOD;
ans = (ans + dfs('u', n, 1)) % MOD;
return (int)ans;
}
private int dfs(char c, int n, int l){
if(l == n)
return 1;
String key = c + "_" + l;
if (memo.containsKey(key)) return memo.get(key);
long res = 0;
if (c == 'a') {
res = dfs('e', n, l + 1);
} else if (c == 'e') {
res = (res + dfs('a', n, l + 1)) % MOD;
res = (res + dfs('i', n, l + 1)) % MOD;
} else if (c == 'i') {
res = (res + dfs('a', n, l + 1)) % MOD;
res = (res + dfs('e', n, l + 1)) % MOD;
res = (res + dfs('o', n, l + 1)) % MOD;
res = (res + dfs('u', n, l + 1)) % MOD;
} else if (c == 'o') {
res = (res + dfs('i', n, l + 1)) % MOD;
res = (res + dfs('u', n, l + 1)) % MOD;
} else {
res = dfs('a', n, l + 1);
}
memo.put(key, (int)(res % MOD));
return (int)(res % MOD);
}
}