当前位置:网站首页>Leetcode每日一题824. 山羊拉丁文
Leetcode每日一题824. 山羊拉丁文
2022-04-21 23:28:00 【小羊努力变强】
写在前面
本篇内容:Leetcode每日一题824. 山羊拉丁文
文章专栏:leetcode每日一题《打卡日常》
算法仓库:小的变强之路
题目
给你一个由若干单词组成的句子 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 <= 150
sentence 由英文字母和空格组成
sentence 不含前导或尾随空格
sentence 中的所有单词由单个空格分隔
思路
找到每一个单词 + 模拟
我们可以对给定的字符串 sentence 进行一次遍历,找出其中的每一个单词,并根据题目的要求进行操作。
在寻找单词时,我们可以使用语言自带的 split() 函数,将空格作为分割字符,得到所有的单词。为了节省空间,我们也可以直接进行遍历:每当我们遍历到一个空格或者到达 sentence 的末尾时,我们就找到了一个单词。
当我们得到一个单词 w 后,我们首先需要判断 w 的首字母是否为元音字母。我们可以使用一个哈希集合 vowels 存储所有的元音字母 aeiouAEIOU,这样只需要判断 w 的首字母是否在 vowels 中。如果是元音字母,那么单词本身保持不变;如果是辅音字母,那么需要首字母移到末尾,这里使用语言自带的字符串切片函数即可。在这之后,我们需要在末尾添加 m 以及若干个 a,因此可以使用一个变量 cnt 记录需要添加的 a 的个数,它的初始值为 1,每当我们得到一个单词,就将它的值增加 1。
代码实现
class Solution {
public:
string toGoatLatin(string sentence) {
unordered_set<char> vowels = {
'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
int n = sentence.size();
int i = 0, cnt = 1;
string ans;
while (i < n) {
int j = i;
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;
}
};
复杂度分析
时间复杂度:O(n2)
空间复杂度:O(n)
写在最后
觉得本篇文章不错的话记得点赞,收藏,还有问题也可以评论留言
你的支持将是我继续创作的最大动力️️️
由于作者水平有限,如有错误和不准确之处在所难免,本人也很想知道这些错误,恳望读者批评指正!
版权声明
本文为[小羊努力变强]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_54847231/article/details/124329779
边栏推荐
- leetcode - 329. Longest increasing path in matrix
- 简约易收录的导航网站源码
- Sélection et évolution des microservices dans l'architecture native du cloud
- Self made webserver from scratch (XVI) -- learn a new tool cmake to automatically write makefile and sort out source files by categories. Feel happy
- 雲原生架構下的微服務選型和演進
- [reprint] postman omysql connection database
- Changsha good man
- golang力扣leetcode 385.迷你语法分析器
- Leetcode:443 Compressed string
- QT custom control 01 simple timer
猜你喜欢

uni-app 图片适配 动态计算图片高度

Sélection et évolution des microservices dans l'architecture native du cloud

GIC spec之ITS和LPI中断5

. 101 keyboard events

通过点击导入的文件或点击组件进入对应的组件页面进行编辑

C language for complete square

(7) Ruixin micro rk3568 builderoot adds compiled scripts and binary program files

Following Huawei Cangjie, it reproduces four domestic programming languages in various forms, including one 0 code

341-Linux 连接数据库

BUUCTF 刷题记录
随机推荐
Brush classic topics
Golang force buckle leetcode 386 Dictionary order
leetcode:440. The k-th smallest digit in dictionary order
【H.264】H.264 解析 工具、web解析
Ruffian Heng embedded: talk about the application and influence of system watchdog wdog1 in the startup of i.mxrt1xxx system
golang力扣leetcode 第 289 场周赛
.100滚轮事件
【H.264】简单编码器及SPS
Golang force buckle leetcode 380 O (1) time insertion, deletion and acquisition of random elements
uni-app 图片适配 动态计算图片高度
经典题目刷一刷
如何构建一个可“持续演进”的可观测体系?| QCon
6、協議層次化和服務模型(重點)
Pytorch (V) -- Notes
87 R k-means,层次聚类,EM聚类的实现
Selection and evolution of microservices under cloud native architecture
leetcode - 329. Longest increasing path in matrix
2022/4/21
There are Chinese characters in the input parameter, and an error of 500 is reported. There is an internal error in the server
The three secret softwares of the leaders are practical and powerful, which are powerful tools for office efficiency and workplace promotion