当前位置:网站首页>[leetcode] daily question: goat Latin
[leetcode] daily question: goat Latin
2022-04-21 19:06:00 【Lonely crossing】
Goat Latin
Give you a sentence consisting of several words sentence , Words are separated by spaces . Each word consists only of uppercase and lowercase letters .
Please translate the sentence into “ Goat Latin (Goat Latin)”( A similar Pig Latin - Pig Latin The fictional language of ). The rules of goat Latin are as follows :
If the word begins with a vowel (‘a’, ‘e’, ‘i’, ‘o’, ‘u’), Add... After the word "ma".
for example , word “apple” Turn into “applema” .
If the word begins with a consonant ( namely , Non vowel letter ), Remove the first character and put it at the end , Then add "ma".
for example , word “goat” Turn into “oatgma” .
According to the index of words in sentences , Add at the end of the word the same number of letters as the index ’a’, Index from 1 Start .
for example , Add... After the first word “a” , Add... After the second word “aa” , And so on .
Return to sentence Sentence after conversion to goat Latin .
source : Power button (LeetCode)
AC Code
class Solution:
def toGoatLatin(self, sentence: str) -> str:
def change(word, idx):
if word[0] in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']:
return word + 'ma' + 'a' * (idx + 1)
else:
return word[1:] + word[0] + 'ma' + 'a' * (idx + 1)
senlist= sentence.split(' ')
res = ''
for idx, word in enumerate(senlist):
if idx < len(senlist) - 1:
res += change(word, idx) + ' '
else:
res += change(word, idx)
return res
Official code
class Solution:
def toGoatLatin(self, sentence: str) -> str:
vowels = {
"a", "e", "i", "o", "u", "A", "E", "I", "O", "U"}
n = len(sentence)
i, cnt = 0, 1
words = list()
while i < n:
j = i
while j < n and sentence[j] != " ":
j += 1
cnt += 1
if sentence[i] in vowels:
words.append(sentence[i:j] + "m" + "a" * cnt)
else:
words.append(sentence[i+1:j] + sentence[i] + "m" + "a" * cnt)
i = j + 1
return " ".join(words)
# author :LeetCode-Solution
The official solution is also the idea of simulation , But it doesn't directly use split This method , Instead, I chose to traverse character by character , It's a return to tradition
版权声明
本文为[Lonely crossing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211900341050.html
边栏推荐
- 2022.04.21(LC_435_无重叠区间)
- Webrtc video cannot be played. How to add UDP hole drilling program in easycvr?
- APM series - industry cognition
- Sleuth+Zipkin链路追踪
- The fifth chapter uses Matplotlib to draw pie chart
- Rk3399—添加usb转串口驱动
- [talkative cloud native] load balancing - the passenger flow of small restaurants has increased
- 电网GPS北斗卫星时间同步系统(时钟装置)技术介绍
- 【C语言进阶】⑥函数指针详解
- 国标GB28181平台EasyGBS如何开启语音对讲功能?
猜你喜欢

108. 将有序数组转换为二叉搜索树(图文并解)
![[ES6] let, const, deconstruction assignment, template string](/img/42/1e57b501799cc019cf652b8aaf5999.gif)
[ES6] let, const, deconstruction assignment, template string

2022.04.21 (lc_56_consolidation interval)

有PMP证书一个月能拿多少钱

MySQL cannot use MySQL - U root - P to start error reporting and solve it

Crystal Chem小鼠葡萄糖检测试剂盒说明书

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

2022.04.21 (lc_435_no overlapping interval)

Database advanced learning: storage engine

Abbexa 山羊 F(ab‘)2 IgG 同种型对照
随机推荐
Svg series - 1, entry level operation
How to call the login, playback address and video playback interface of easycvr platform?
Database advanced learning: index classification and creation syntax
2021年拍卖行业发展研究报告
leetcode:423. Reconstructing numbers from English
SIGIR'22「阿里」MetaCVR:元学习缓解小规模推荐中数据分布波动问题
"Actual combat" realizes linear regression with tensorflow
Qianxin monitoring equipment --- Jiaotu
NMI paper by payel DAS and others of IBM Research Institute: general machine learning framework for optimizing molecules
刚毕业大学生考PMP用处大吗?
DVWA-Brute Force
Chinese NER Using Lattice LSTM 论文解读
直面阿里的勇气
To deal with doget & dopost Chinese garbled code
APM 行业认知系列 - 二
排序会了递归,不学非递归太可惜了
Leetcode 824. Goat Latin
JS -- new date() instance
108. 将有序数组转换为二叉搜索树(图文并解)
全职加入清华,丘成桐:为祖国、为全球数学界培养数学人才