forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goat-latin.cpp
33 lines (32 loc) · 977 Bytes
/
goat-latin.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
// Time: O(n + w^2), n = w * l,
// n is the length of S,
// w is the number of words,
// l is the average of word lengths
// Space: O(l)
class Solution {
public:
string toGoatLatin(string S) {
unordered_set<char> vowel{'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
string result, word;
int count = 0;
for (int i = 0; i < S.length(); ++i) {
if (S[i] != ' ') {
word.push_back(S[i]);
if (i != S.length() - 1) {
continue;
}
}
if (!vowel.count(word.front())) {
word.push_back(word.front());
word = word.substr(1);
}
word += "ma";
word += string(++count, 'a');
result += word;
result += " ";
word.clear();
}
result.pop_back();
return result;
}
};