-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountSortedVowelString.java
41 lines (33 loc) · 985 Bytes
/
CountSortedVowelString.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
/**
*
* Given an integer n, return the number of strings of length n that consist only of vowels (a, e, i, o, u) and are lexicographically sorted.
*
* A string s is lexicographically sorted if for all valid i, s[i] is the same as or comes before s[i+1] in the alphabet.
*/
public class CountSortedVowelString {
char[] vowels={'a','e','i','o','u'};
public int countVowelStrings(int n) {
int res=0;
for (char v:vowels) {
res+=count(n-1,v);
}
return res;
}
private int count(int len, char v) {
if(len==0)
return 1;
int temp=0;
for (char vowel:
vowels) {
if(vowel>=v)
temp+=count(len-1,vowel);
}
return temp;
}
public static void main(String[] args) {
CountSortedVowelString obj=new CountSortedVowelString();
int n=2;
int i = obj.countVowelStrings(n);
System.out.println(i);
}
}