824. Goat Latin
题目
A sentence S
is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only.
We would like to convert the sentence to “Goat Latin” (a made-up language similar to Pig Latin.)
The rules of Goat Latin are as follows:
If a word begins with a vowel (a, e, i, o, or u), append "ma"
to the end of the word.
For example, the word "apple"
becomes "applema"
.
If a word begins with a consonant (i.e. not a vowel), remove the first letter and append it to the end, then add "ma"
.
For example, the word "goat"
becomes "oatgma"
.
Add one letter "a"
to the end of each word per its word index in the sentence, starting with 1.
For example, the first word gets "a"
added to the end, the second word gets "aa"
added to the end and so on.
Return the final sentence representing the conversion from S
to Goat Latin.
Example 1:
Input: "I speak Goat Latin"
Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
Example 2:
Input: "The quick brown fox jumped over the lazy dog"
Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
Notes:
S
contains only uppercase, lowercase and spaces. Exactly one space between each word.1 <= S.length <= 150
.
解决
设S
的长度为n
,有m
个单词(n > m
)。
1.暴力解决
利用istringstream
提取S
中的每个单词,依次进行加工,得到目标字符串。
- 时间复杂度:O(n * m)
- 空间复杂度:O(n)
class Solution {
public:
string toGoatLatin(string S) {
string result;
vector<string> words = getWord(S);
int n = words.size();
for (int i = 0; i < n; i++) {
if (isVowel(words[i][0])) {
words[i] += "ma";
} else {
string temp = words[i].substr(1);
words[i] = temp + words[i][0] + "ma";
}
int count = i + 1;
while (count--) {
words[i] += "a";
}
if (i == n - 1) {
result += words[i];
} else {
result += words[i] + " ";
}
}
return result;
}
vector<string> getWord(string S) {
vector<string> words;
istringstream ss(S);
string temp;
while (ss >> temp) {
words.push_back(temp);
}
return words;
}
bool isVowel(char c) {
vector<char> letter = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
unordered_set<char> vowel(letter.begin(), letter.end());
return vowel.count(c);
}
};
2.暴力解决的优化
优化方案:利用string
库中的函数简化对word
处理。主要使用append()
函数来避免上面方法中的while
循环。
- 时间复杂度:O(n)
- 空间复杂度:O(n)
class Solution {
public:
string toGoatLatin(string S) {
string result;
vector<string> words;
istringstream ss(S);
string temp;
while (ss >> temp) {
words.push_back(temp);
}
map<char, int> vowel;
vowel['a'] = 1;
vowel['A'] = 1;
vowel['e'] = 1;
vowel['E'] = 1;
vowel['i'] = 1;
vowel['I'] = 1;
vowel['o'] = 1;
vowel['O'] = 1;
vowel['u'] = 1;
vowel['U'] = 1;
int n = words.size();
for (int i = 0; i < n; i++) {
if (!vowel[words[i][0]]) {
char temp = words[i][0];
words[i].erase(0, 1);
result += words[i] + temp;
} else {
result += words[i];
}
result += "ma";
// 在字符串末尾添加i+1个a
result.append(i + 1, 'a');
if (i != n - 1) result += " ";
}
return result;
}
};
3.stringstream快速解决
stringstream
可以类似我们平常使用cout
和cin
。stringstream
的>>
可以根据string
的空格进行分割,<<
可以将string
拼接起来。
stringstream
有函数str()
使得stream buffer
转为string
。
- 时间复杂度:O(m)
- 空间复杂度:O(n)
class Solution {
public:
string toGoatLatin(string S) {
stringstream iss(S), oss;
string vowels("aeiouAEIOU"), word, a;
while (iss >> word) {
a.push_back('a');
if (vowels.find_first_of(word[0]) != string::npos) {
oss << ' ' << word << "ma" << a;
} else {
oss << ' ' << word.substr(1) << word[0] << "ma" << a;
}
}
return oss.str().substr(1);
}
};