当前位置:网站首页>824.山羊拉丁文
824.山羊拉丁文
2022-04-21 20:32:00 【Phoenix_ZengHao】
题目
824.山羊拉丁文
题目大意
给你一个由若干单词组成的句子 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中的所有单词由单个空格分隔
思路
题意很简单,就是对第 i i i个单词末端加入字符串:( i ∈ [ 1 , n ] i\in [1,n] i∈[1,n])
- 如果开头是元音,就直接加入
"ma",并且末尾加入 i i i个'a' - 否则,将开头字符移到末尾,加入
"ma",并且末尾加入 i i i个'a'
使用C++我感觉分割有点麻烦,于是我直接上python,ls存储每个单词,now表示当前是第now个单词,如果l[0]是元音,直接l+'ma'+'a'*now,否则l[1:]+l[0]+'ma'+'a'*now,然后将其加入到ans数组中,最后返回答案即可。不得不说,python在针对字符串类型的题目的时候的确比C++方便很多。
class Solution:
def toGoatLatin(self, sentence: str) -> str:
ls=sentence.split()
ans=[]
now=1
for l in ls:
a=""
if(l[0].lower() in ['a', 'e', 'i', 'o', 'u']):
a=l+'ma'+'a'*now
else:
a=l[1:]+l[0]+'ma'+'a'*now
now+=1
ans.append(a)
return ' '.join(ans)
版权声明
本文为[Phoenix_ZengHao]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Phoenix_ZengHao/article/details/124324675
边栏推荐
- <2021SC@SDUSC>山东大学软件工程应用与实践JPress代码分析(一)
- IaaS,PaaS,SaaS 的区别
- Jerry's VDDIO_ SYSVDD_ DC14 system voltage configuration description [chapter]
- Share the advantages of Intranet instant messaging software
- 3D 沙盒游戏之人物的点击行走移动
- After three years of graduation, he achieved nothing and was forced to go back to his hometown. He made a decision to change his life.
- Code out efficiency Chapter 7 concurrency and multithreading
- Jerry's interrupt list [chapter]
- 【Azure 应用服务】Azure Function 启用 Managed Identity后, Powershell Funciton出现 ERROR: ManagedIdentityCredential authentication failed
- Andorid - - Pourquoi utiliser une transaction, qu'est - ce qu'une transaction commit et ROLLBACK?
猜你喜欢

Specific methods of configuring Profibus and PROFINET communication in two TIA botu projects

3D 沙盒游戏之人物的点击行走移动

长安深蓝C385产品信息曝光 瞄准20万级别,头号目标Model 3!

分布式秒杀系统构建

C语言求完全平方数

Code out efficiency Chapter 7 concurrency and multithreading

C语言:简单的利润与奖金

深度剖析TCP三次握手,面试官拍案叫绝

Know that Chuangyu issued a heavy strategic plan to build a practical defense system for continuous exchange of fire

android开发实习面试题,安卓开发面试基础
随机推荐
composer的源切换
oracle数据导入记录笔记
android开发实习面试题,安卓开发面试基础
JUC queue interface and its implementation class
Vtkjs introduction
再谈企业信息化的本质
How to ensure the stability and correctness of API? That's all you need
C # cannot be used for characters of file name
Channel allocation don't use the four-color theorem
高效c语言 内存拷贝. 测试结果 rand, loop, operator= % in x86-64 SUSE
单、双链表的循环链表(十五)
实战 | 基于JMeter 完成典型电商场景(首页浏览)的性能压测
redis 高可用(ha)
In depth analysis of TCP three handshakes, the interviewer applauded
[transfer] SSE2 SSE introduction and C code example
The difference and relationship between glew, glee and GL Glu glut GLX glext
Introduction to WLAN qpower
解决composer报错:Could not find a version of package xxx/yyy
Man machine verification reCAPTCHA V3 complete instructions
【原】biginteger。大数乘法。大数运算。“无限大数字”乘法。大数乘法两种方法对比