-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoGoatLatin.cpp
44 lines (34 loc) · 1.02 KB
/
toGoatLatin.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
class Solution {
bool is_vowel(char c) {
char lc = tolower(c);
return lc == 'a' || lc == 'e' || lc == 'i' || lc == 'o' || lc == 'u';
}
vector<string> split(string S) {
vector<string> result;
string subs = "";
for(char c: S) {
if(c != ' ') subs += c;
else {
result.push_back(subs);
subs = "";
}
}
result.push_back(subs);
return result;
}
public:
string toGoatLatin(string S) {
vector<string> words = split(S);
string result = "";
string suff = "";
for(int i = 0; i < words.size(); ++i) {
string word = words[i];
suff.push_back('a');
if(!is_vowel(word[0])) result += word.substr(1) + word[0];
else result += word;
result += "ma" + suff + " ";
}
result.pop_back();
return result;
}
};