-
Notifications
You must be signed in to change notification settings - Fork 20
/
longest-vowels-subsequence.cpp
72 lines (70 loc) · 2.18 KB
/
longest-vowels-subsequence.cpp
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
/**
Problem statement
a string s consists of only vowels: a, e, i, o, u
Find a subsequence in the string s such that it consists of all five vowels and is a sequence of
one or more a's, followed by one or more e's, followed by one or more i's, followed by one or
more o's, followed by one or more u's.
Example: a e i a a i o o o a a u u a e i o u
Longest subsequence: a e i i o o o u u u -> Length = 10
If such a subsequence can't be found, return 0;
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "aeiaaioooaauuaeiou";
int n = s.size(), longest[n], last['z'+1], unique[n];
longest[0] = 1;
for (int i = 1; i < n; i++) {
unique[i] = 1;
if (s[i] == s[i-1]) {
longest[i] = longest[i-1] + 1;
} else {
longest[i] = 1;
}
}
for (int i = 0; i < n; i++) {
if ('a' <= s[i]) {
if (longest[last['a']] + 1 >= longest[s[i]]) {
longest[i] = longest[last['a']] + 1;
if (s[i] != 'a') unique[i] = max(unique[i], unique[last['a']] + 1);
else unique[i] = max(unique[i], unique[last['a']]);
}
}
if ('e' <= s[i]) {
if (longest[last['e']] + 1 >= longest[s[i]]) {
longest[i] = longest[last['e']] + 1;
if (s[i] != 'e') unique[i] = max(unique[i], unique[last['e']] + 1);
else unique[i] = max(unique[i], unique[last['e']]);
}
}
if ('i' <= s[i]) {
if (longest[last['i']] + 1 >= longest[s[i]]) {
longest[i] = longest[last['i']] + 1;
if (s[i] != 'i') unique[i] = max(unique[i], unique[last['i']] + 1);
else unique[i] = max(unique[i], unique[last['i']]);
}
}
if ('o' <= s[i]) {
if (longest[last['o']] + 1 >= longest[s[i]]) {
longest[i] = longest[last['o']] + 1;
if (s[i] != 'o') unique[i] = max(unique[i], unique[last['o']] + 1);
else unique[i] = max(unique[i], unique[last['o']]);
}
}
if ('u' <= s[i]) {
if (longest[last['u']] + 1 >= longest[s[i]]) {
longest[i] = longest[last['u']] + 1;
if (s[i] != 'u') unique[i] = max(unique[i], unique[last['u']] + 1);
else unique[i] = max(unique[i], unique[last['u']]);
}
}
last[s[i]] = i;
}
int result = 0;
for (int i = 0; i < n; i++) {
if (unique[i] == 5 && result < longest[i]) {
result = longest[i];
}
}
return result;
}