Easy
题目描述
给你一个由若干单词组成的句子 sentence,单词间由空格分隔。每个单词仅由大写和小写英文字母组成。
请你将句子转换为"山羊拉丁文(Goat Latin)"(一种类似于猪拉丁文 - Pig Latin 的虚构语言)。山羊拉丁文的规则如下:
如果单词以元音开头(‘a’, ’e’, ‘i’, ‘o’, ‘u’),在单词后添加
"ma"。- 例如,单词
"apple"变为"applema"。
- 例如,单词
如果单词以辅音开头(即,不是元音),移除第一个字母并将它放到末尾,然后添加
"ma"。- 例如,单词
"goat"变为"oatgma"。
- 例如,单词
根据单词在句子中的索引,在单词最后添加与索引相同数量的字母
'a',索引从 1 开始。- 例如,在第一个单词的末尾添加
"a",在第二个单词的末尾添加"aa",以此类推。
- 例如,在第一个单词的末尾添加
返回将 sentence 转换为山羊拉丁文后的句子。
示例 1:
输入:sentence = "I speak Goat Latin"
输出:"Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
示例 2:
输入:sentence = "The quick brown fox jumped over the lazy dog"
输出:"heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
提示:
1 <= sentence.length <= 150sentence由英文字母和空格组成sentence不含前导空格和尾随空格sentence中所有单词由单个空格分隔
解题思路
这是一道字符串处理题,我们需要按照题目给出的三个规则来转换每个单词。
解题思路:
- 分割句子:首先将输入的句子按空格分割成单词列表
- 逐个处理单词:对每个单词按照以下规则处理:
- 判断首字母是否为元音(a, e, i, o, u,注意大小写)
- 如果是元音:直接在单词后加上"ma"
- 如果是辅音:将首字母移到末尾,然后加上"ma"
- 根据单词在句子中的位置(从1开始),在末尾添加相应数量的字母’a’
- 拼接结果:将所有处理后的单词用空格连接
实现要点:
- 元音判断时需要考虑大小写字母
- 单词索引从1开始,第i个单词需要添加i个’a’
- 字符串拼接可以用StringBuilder提高效率
时间复杂度分析:
- 遍历所有单词:O(n),其中n是单词总数
- 处理每个单词:O(m),其中m是单词长度
- 总体时间复杂度:O(n×m),其中n是单词数,m是平均单词长度
代码实现
class Solution {
public:
string toGoatLatin(string sentence) {
vector<string> words;
stringstream ss(sentence);
string word;
// 分割单词
while (ss >> word) {
words.push_back(word);
}
string result = "";
for (int i = 0; i < words.size(); i++) {
string current = words[i];
char first = tolower(current[0]);
// 判断是否以元音开头
if (first == 'a' || first == 'e' || first == 'i' ||
first == 'o' || first == 'u') {
current += "ma";
} else {
// 辅音开头:移除首字母并添加到末尾
current = current.substr(1) + current[0] + "ma";
}
// 添加对应数量的'a'
for (int j = 0; j <= i; j++) {
current += 'a';
}
if (i > 0) result += " ";
result += current;
}
return result;
}
};
class Solution:
def toGoatLatin(self, sentence: str) -> str:
words = sentence.split()
result = []
vowels = set('aeiouAEIOU')
for i, word in enumerate(words):
# 检查首字母是否为元音
if word[0] in vowels:
new_word = word + "ma"
else:
# 辅音开头:移除首字母并添加到末尾
new_word = word[1:] + word[0] + "ma"
# 添加对应数量的'a'(索引从0开始,所以是i+1个)
new_word += 'a' * (i + 1)
result.append(new_word)
return ' '.join(result)
public class Solution {
public string ToGoatLatin(string sentence) {
string[] words = sentence.Split(' ');
StringBuilder result = new StringBuilder();
HashSet<char> vowels = new HashSet<char> {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
for (int i = 0; i < words.Length; i++) {
string word = words[i];
string newWord;
// 判断首字母是否为元音
if (vowels.Contains(word[0])) {
newWord = word + "ma";
} else {
// 辅音开头:移除首字母并添加到末尾
newWord = word.Substring(1) + word[0] + "ma";
}
// 添加对应数量的'a'
newWord += new string('a', i + 1);
if (i > 0) result.Append(" ");
result.Append(newWord);
}
return result.ToString();
}
}
var toGoatLatin = function(sentence) {
const words = sentence.split(' ');
const vowels = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
const result = [];
for (let i = 0; i < words.length; i++) {
const word = words[i];
let newWord;
// 判断首字母是否为元音
if (vowels.has(word[0])) {
newWord = word + "ma";
} else {
// 辅音开头:移除首字母并添加到末尾
newWord = word.slice(1) + word[0] + "ma";
}
// 添加对应数量的'a'
newWord += 'a'.repeat(i + 1);
result.push(newWord);
}
return result.join(' ');
};
复杂度分析
| 复杂度类型 | 复杂度 | 说明 |
|---|---|---|
| 时间复杂度 | O(n×m) | n为单词数量,m为平均单词长度,需要遍历所有单词并处理每个字符 |
| 空间复杂度 | O(n×m) | 存储分割后的单词列表和结果字符串,空间与输入大小线性相关 |