[LeetCode&Python] Problem 824. Goat Latin
A sentence S
is given, composed 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 S
to Goat Latin.
Example 1:
Input: "I speak Goat Latin"
Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
Example 2:
Input: "The quick brown fox jumped over the lazy dog"
Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
Notes:
S
contains only uppercase, lowercase and spaces. Exactly one space between each word.1 <= S.length <= 150
.
class Solution:
def toGoatLatin(self, S):
"""
:type S: str
:rtype: str
"""
def is_vowel(c):
return c=='a' or c=='e' or c=='i' or c=='o' or c=='u' or c=='A' or c=='E' or c=='I' or c=='O' or c=='U' l=S.split(' ')
n=len(l) for i in range(n):
s=l[i]
if is_vowel(s[0]):
l[i]=l[i]+'ma'
else:
fl=l[i][0]
l[i]=l[i][1:]
l[i]=l[i]+fl+'ma'
astr='a'*(i+1)
l[i]=l[i]+astr return " ".join(l)
[LeetCode&Python] Problem 824. Goat Latin的更多相关文章
- 【Leetcode_easy】824. Goat Latin
problem 824. Goat Latin solution class Solution { public: string toGoatLatin(string S) { unordered_s ...
- 824. Goat Latin - LeetCode
Questioin 824. Goat Latin Solution 题目大意:根据要求翻译句子 思路:转换成单词数组,遍历数组,根据要求转换单词 Java实现: 用Java8的流实现,效率太低 pu ...
- 【LeetCode】824. Goat Latin 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 824 Goat Latin 解题报告
题目要求 A sentence S is given, composed of words separated by spaces. Each word consists of lowercase a ...
- [LeetCode] 824. Goat Latin
Description A sentence S is given, composed of words separated by spaces. Each word consists of lowe ...
- 824. Goat Latin山羊拉丁文
[抄题]: A sentence S is given, composed of words separated by spaces. Each word consists of lowercase ...
- LeetCode 824. Goat Latin (山羊拉丁文)
题目标签:String 首先把vowel letters 保存入 HashSet. 然后把S 拆分成 各个 word,遍历每一个 word: 当 word 第一个 字母不是 vowel 的时候,把第一 ...
- [LeetCode&Python] Problem 520. Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...
- [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
随机推荐
- shiro过滤器解释类
anon -- org.apache.shiro.web.filter.authc.AnonymousFilter authc -- org.apache.shiro.web.filter.authc ...
- python xlrd使用
python xlrd使用 1● xlrd安装 管理员模式 success 2● 引用 import xlrd
- 逆袭之旅DAY.XIA.Object中常用方法
2018-07-31
- 普通程序员,三年成为年薪70w架构师,只因做到了这些
每个程序员.或者说每个工作者都应该有自己的职业规划,如果你不是富二代,不是官二代,也没有职业规划,希望你可以思考一下自己的将来.今天给大家分享的是一篇来自阿里Java架构师对普通程序员的职业建议,希望 ...
- 获取音、视频时长(NAudio,Shell32,FFmpeg)
参考网址:https://blog.csdn.net/u013810234/article/details/57471780 以下为本次测试用到的音.视频格式: audio :”.wav;.mp3;. ...
- U帮忙U盘启动盘制作
第一步:制作U盘启动盘前的软.硬件准备 1.准备一个U盘或内存卡(尽量使用2G以上的) 2.进入 U帮忙官网 下载最新版U盘启动盘制作工具! 3.搜索并下载ghost版系统文件存放到电脑中. 第二步: ...
- 每天CSS学习之border-radius
css3的border-radius属性,我们用之来画圆角边框. 1.border-radius:none;//表示不用圆角边框,边框会变成方形. 2.border-radius:水平方向{1,4}[ ...
- 4.5 C++重载、覆盖和遮蔽
参考:http://www.weixueyuan.net/view/6375.html 总结: 函数签名包括函数名和函数参数的个数.顺序以及参数数据类型. 需要注意的是函数签名并不包含函数返回值部分, ...
- css经验之谈
//清除浮动 .clear { zoom:1; }.clear:after { content:''; display:block; clear:both; }.fl { float:left; }. ...
- MySQL输入密码后闪退
刚刚我遇到这个问题,服务里MySQL是启状态的,所以我求助百度,发现很多种说法,我试了几个,还是不行,后来想起来我的密码不对,于是换了正确的密码试了一下,没毛病,进去了. 所以输入密码闪退时,首先确定 ...