当前位置:网站首页>Leetcode 824. Goat Latin
Leetcode 824. Goat Latin
2022-04-21 18:59:00 【cwtnice】
Original link :Leetcode 824. Goat Latin
You are given a string sentence that consist 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 sentence to Goat Latin.
Example 1:
Input: sentence = "I speak Goat Latin"
Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
Example 2:
Input: sentence = "The quick brown fox jumped over the lazy dog"
Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
Constraints:
- 1 <= sentence.length <= 150
- sentence consists of English letters and spaces.
- sentence has no leading or trailing spaces.
- All the words in sentence are separated by a single space.
Method 1 : simulation
c++ Code :
class Solution {
public:
string toGoatLatin(string sentence) {
// Vowel set use set For the convenience of judgment
unordered_set<char> vowels = {
'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
int n = sentence.size();
// cnt Number of words
int i = 0, cnt = 1;
string ans;
while (i < n) {
int j = i;
// Find a word
while (j < n && sentence[j] != ' ') {
++j;
}
++cnt;
if (cnt != 2) {
ans += ' ';
}
if (vowels.count(sentence[i])) {
ans += sentence.substr(i, j - i) + 'm' + string(cnt, 'a');
}
else {
ans += sentence.substr(i + 1, j - i - 1) + sentence[i] + 'm' + string(cnt, 'a');
}
i = j + 1;
}
return ans;
}
};
版权声明
本文为[cwtnice]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211852263562.html
边栏推荐
- Splicing method of fields in MySQL database and Oracle Database
- 奇安信监控设备 --- 椒图
- 使用MCUXpresso开发RT1060(1)——驱动RGB接口LCD
- 2021年拍卖行业发展研究报告
- Tiandan released the cloud native intelligent operation and maintenance product line and launched the world's first full stack observability technical standard
- [04] [02] [02] SPI mechanism
- 【王道考研3】OSI七层参考模型,TCP/IP参考模型和5层参考模型
- 2022.04.21(LC_56_合并区间)
- Understand the new economic model of platofarm and its ecological progress
- 深度学习多机多卡batchsize和学习率的关系
猜你喜欢
![[04] [01] [01] redis Basics](/img/35/835ea69773be0ca6c238ffa3d33b2a.png)
[04] [01] [01] redis Basics

NMI paper by payel DAS and others of IBM Research Institute: general machine learning framework for optimizing molecules

How much can I get a month with a PMP certificate

毕业三年,一事无成,被迫回老家,一个决定改变一生。

Abbexa MPO (FITC) / CD3 (PE) 组合抗体

Typescript quick start, class, public, private, extensions

有什么蓝牙耳机不贵又实用?适合学生党的无线蓝牙耳机

SVG系列——1,入门级操作

Channel Allocation 别用四色定理

为什么你做数据分析没思路?
随机推荐
How much can I get a month with a PMP certificate
干货 | 面试过了,如何谈薪资?拿高薪Offer有哪些技巧?
flink分流
Exness: Netflix reported the loss of users for the first time in a decade, and its share price plunged 25%
Understand the new economic model of platofarm and its ecological progress
How to be a good project manager?
2022.04.21(LC_56_合并区间)
mysql不能使用 mysql -u root -p 启动报错解决
Which wireless Bluetooth headset is easier to use? 2022 Bluetooth headset recommendations
【王道考研3】OSI七层参考模型,TCP/IP参考模型和5层参考模型
【无标题】
【大话云原生】负载均衡篇-小饭馆客流量变大了
体育场馆系统可以连接其他智能设备吗
Svg series - 2, draw some shapes
PAT (Advanced Level) 1096——Consecutive连续的
Database advanced learning: storage engine
Introduction to IPv6 foundation and common commands
Use the replay function of chrome to publish a blog quickly
新内核版本的EasyCVR如何设置ts切片文件大小?
How does Tianchuang Suying charge for static creativity?