Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d58d93b

Browse files
committed
Create 0824. 山羊拉丁文.md
1 parent 1641a9e commit d58d93b

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

‎Solutions/0824. 山羊拉丁文.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# [0824. 山羊拉丁文](https://leetcode.cn/problems/goat-latin/)
2+
3+
- 标签:字符串
4+
- 难度:简单
5+
6+
## 题目大意
7+
8+
**描述**:给定一个由若干单词组成的句子 $sentence,ドル单词之间由空格分隔。每个单词仅由大写和小写字母组成。
9+
10+
**要求**:将句子转换为「山羊拉丁文(Goat Latin)」,并返回将 $sentence$ 转换为山羊拉丁文后的句子。
11+
12+
**说明**:
13+
14+
- 山羊拉丁文的规则如下:
15+
- 如果单词以元音开头(`a`,`e`,`i`,`o`,`u`),在单词后添加 `"ma"`
16+
- 例如,单词 `"apple"` 变为 `"applema"`
17+
18+
- 如果单词以辅音字母开头(即,非元音字母),移除第一个字符并将它放到末尾,之后再添加 `"ma"`
19+
- 例如,单词 `"goat"` 变为 `"oatgma"`
20+
21+
- 根据单词在句子中的索引,在单词最后添加与索引相同数量的字母 `a`,索引从 1ドル$ 开始。
22+
- 例如,在第一个单词后添加 `"a"` ,在第二个单词后添加 `"aa"`,以此类推。
23+
24+
- 1ドル \le sentence.length \le 150$。
25+
- $sentence$ 由英文字母和空格组成。
26+
- $sentence$ 不含前导或尾随空格。
27+
- $sentence$ 中的所有单词由单个空格分隔。
28+
29+
**示例**:
30+
31+
- 示例 1:
32+
33+
```Python
34+
输入:sentence = "I speak Goat Latin"
35+
输出:"Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
36+
```
37+
38+
- 示例 2:
39+
40+
```Python
41+
输入:sentence = "The quick brown fox jumped over the lazy dog"
42+
输出:"heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
43+
```
44+
45+
## 解题思路
46+
47+
### 思路 1:模拟
48+
49+
1. 使用集合 $vowels$ 存储元音字符,然后将 $sentence$ 按照空格分隔成单词数组 $words$。
50+
2. 遍历单词数组 $words,ドル对于当前单词 $word,ドル根据山羊拉丁文的规则,将其转为山羊拉丁文的单词,并存入答案数组 $res$ 中。
51+
3. 遍历完之后将答案数组拼接为字符串并返回。
52+
53+
### 思路 1:代码
54+
55+
```Python
56+
class Solution:
57+
def toGoatLatin(self, sentence: str) -> str:
58+
vowels = set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'])
59+
words = sentence.split(' ')
60+
res = []
61+
for i in range(len(words)):
62+
word = words[i]
63+
ans = ""
64+
if word[0] in vowels:
65+
ans += word + "ma"
66+
else:
67+
ans += word[1:] + word[0] + "ma"
68+
ans += 'a' * (i + 1)
69+
res.append(ans)
70+
71+
return " ".join(res)
72+
```
73+
74+
### 思路 1:复杂度分析
75+
76+
- **时间复杂度**:$O(n)$。
77+
- **空间复杂度**:$O(1)$。
78+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /