-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountSortedVowelStrings.java
32 lines (27 loc) · 1.01 KB
/
CountSortedVowelStrings.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
package solutions;
// [Problem] https://leetcode.com/problems/count-sorted-vowel-strings
class CountSortedVowelStrings {
// Dynamic programming
// O(n) time, O(1) space
public int countVowelStrings(int n) {
int aCount = 1, eCount = 1, iCount = 1, oCount = 1, uCount = 1;
int numLetters = 2;
while (numLetters <= n) {
aCount = aCount + eCount + iCount + oCount + uCount;
eCount = eCount + iCount + oCount + uCount;
iCount = iCount + oCount + uCount;
oCount = oCount + uCount;
// uCount is always 1
numLetters++;
}
return (aCount + eCount + iCount + oCount + uCount);
}
// Test
public static void main(String[] args) {
CountSortedVowelStrings solution = new CountSortedVowelStrings();
int input = 3;
int expectedOutput = 35;
int actualOutput = solution.countVowelStrings(input);
System.out.println("Test passed? " + (expectedOutput == actualOutput));
}
}