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的更多相关文章

  1. 【Leetcode_easy】824. Goat Latin

    problem 824. Goat Latin solution class Solution { public: string toGoatLatin(string S) { unordered_s ...

  2. 824. Goat Latin - LeetCode

    Questioin 824. Goat Latin Solution 题目大意:根据要求翻译句子 思路:转换成单词数组,遍历数组,根据要求转换单词 Java实现: 用Java8的流实现,效率太低 pu ...

  3. 【LeetCode】824. Goat Latin 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. LeetCode 824 Goat Latin 解题报告

    题目要求 A sentence S is given, composed of words separated by spaces. Each word consists of lowercase a ...

  5. [LeetCode] 824. Goat Latin

    Description A sentence S is given, composed of words separated by spaces. Each word consists of lowe ...

  6. 824. Goat Latin山羊拉丁文

    [抄题]: A sentence S is given, composed of words separated by spaces. Each word consists of lowercase ...

  7. LeetCode 824. Goat Latin (山羊拉丁文)

    题目标签:String 首先把vowel letters 保存入 HashSet. 然后把S 拆分成 各个 word,遍历每一个 word: 当 word 第一个 字母不是 vowel 的时候,把第一 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. ssl tls 证书链 根证书和叶证书查询

    你基本上需要做的是构建一个证书链,如果你没有得到它作为一个链.证书链基本上由第零个位置的最终实体证书(也是叶证书,链中最重要的证书)组成,其次是次要证书. CA证书是最不重要的. 所以这是通常的X.5 ...

  2. 你应该这样理解JVM内存管理

    在进行Java程序设计时,一般不涉及内存的分配和内存回收的相关代码,此处引用一句话: Java和C++之间存在一堵由内存动态分配和垃圾收集技术所围成的高墙,墙外的人想进去,墙里面的人想出来 ,个人从这 ...

  3. 整数中1出现的次数(1~n)

    题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...

  4. centos6.5+python2.7+flask+apache+mod-wsgi部署

    flask部署,使用的是centos6.5,python2.7,版本很重要.基本步骤如下: 一.创建虚拟环境,创建目录把项目拷进去 二.安装mod-wsgi和apache easy_install m ...

  5. bzoj1045

    题解: 随便推一下公式 然后发现是中位数 代码: #include<bits/stdc++.h> using namespace std; ],n; long long sum; int ...

  6. python 数据如何保存到excel中--xlwt

    第一步:下载xlwt 首先要下载xlwt,(前提是你已经安装好了Python) 下载地址:  https://pypi.python.org/pypi/xlwt/   下载第二个   第二步:安装xl ...

  7. MATLAB图片折腾1

    MATLAB 把文件夹里图片转成mat文件 pt='/Users/haoyuguo/Desktop/sync1/'; ext='*.jpg'; dis=dir([pt ext]); nms={dis. ...

  8. Java:将数据库数据导出到Excel (一眼就看会)

    所用Jar包 1. sqljdbc4.jar 连接数据库的Jar包(根据数据库的不同进行选择,我用的SqlServer2008) 2.Jxl.jar 访问Excel的Jar包 注意:支持以.xls结尾 ...

  9. 2.7 C++构造函数

    参考:http://www.weixueyuan.net/view/6339.html 总结: 如果在类中声明了任何一个构造函数,则系统不会自动生成默认构造函数.构造函数同样能够使用类中的成员变量. ...

  10. tensorflow-可视化

    先学习几个英文单词 summary 汇总,摘要 scope 范围 我这是很早以前的笔记,后来有了博客才发布的,有些内容比较老,懒得改了.  先说明总体流程 暂时不管怎么编程,假设已经有了如下代码,可执 ...