【LeetCode】824. Goat Latin 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/goat-latin/description/
题目描述
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.
题目大意
要把英语翻译成Goat Latin语。翻译规则如下:
把英文句子按照空格分割成各个单词。
- 如果单词以元音字母开头,后面接上
"ma"
; - 如果不以元音开头,把首字母移到最后,然后接上
"ma"
; - 每个单词后面接上在句子中出现的第几位置个
"a"
。
解题方法
python天生的适合处理字符串问题。这种纯粹的拼操作的,没有思考的题目,分分钟搞定啊~
太简单了,不讲了。
class Solution:
def toGoatLatin(self, S):
"""
:type S: str
:rtype: str
"""
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
words = S.split(' ')
new_words = []
for i, word in enumerate(words):
if word[0] in vowels:
word += 'ma'
else:
word = word[1:] + word[0] + 'ma'
word += 'a' * (i + 1)
new_words.append(word)
return ' '.join(new_words)
日期
2018 年 5 月 27 日 —— 周末的天气很好~
2018 年 11 月 9 日 —— 睡眠可以
【LeetCode】824. Goat Latin 解题报告(Python)的更多相关文章
- 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 ...
- LeetCode 824. Goat Latin (山羊拉丁文)
题目标签:String 首先把vowel letters 保存入 HashSet. 然后把S 拆分成 各个 word,遍历每一个 word: 当 word 第一个 字母不是 vowel 的时候,把第一 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 824. Goat Latin - LeetCode
Questioin 824. Goat Latin Solution 题目大意:根据要求翻译句子 思路:转换成单词数组,遍历数组,根据要求转换单词 Java实现: 用Java8的流实现,效率太低 pu ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【Leetcode_easy】824. Goat Latin
problem 824. Goat Latin solution class Solution { public: string toGoatLatin(string S) { unordered_s ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
随机推荐
- C/C++ Qt StringListModel 字符串列表映射组件
StringListModel 字符串列表映射组件,该组件用于处理字符串与列表框组件中数据的转换,通常该组件会配合ListView组件一起使用,例如将ListView组件与Model模型绑定,当Lis ...
- Express中间件原理详解
前言 Express和Koa是目前最主流的基于node的web开发框架,他们的开发者是同一班人马.貌似现在Koa更加流行,但是仍然有大量的项目在使用Express,所以我想通过这篇文章说说Expres ...
- Docker学习(四)——Docker容器连接
Docker容器连接 容器中可以运行一些网络应用,要让外部也可以访问这些应用,可以通过-P或-p参数来指定端口映射. 下面我们来实现通过端口连接到一个docker容器. 1.网络端口映射 ...
- C++之无子数
题目如下: 1 #include <iostream> 2 3 using namespace std; 4 5 6 bool isThisNumhaveChild(int num); 7 ...
- vim编码设置(转)
vim里面的编码主要跟三个参数有关:enc(encoding).fenc(fileencoding).fence(fileencodings) fenc是当前文件的编码,也就是说,一个在vim里面已经 ...
- liunux 6.5设置网卡默认开启
编辑如下文件; vi /etc/sysconfig/network-scripts/ifcfg-eth0 把 ONBOOT=no 改为 ONBOOT=yes 好了网卡会在启动机器的时候一起启动了.
- OpenStack之三: 安装MySQL,rabbitmq, memcached
官网地址:https://docs.openstack.org/install-guide/environment-sql-database-rdo.html #:安装mysql [root@mysq ...
- MySQL(5):安装MySQL
下载地址 下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19-winx64.zip 安装步骤 第一步:下载得到压缩包,并解压 ...
- 【编程思想】【设计模式】【行为模式Behavioral】迭代器模式iterator
Python版 https://github.com/faif/python-patterns/blob/master/behavioral/iterator.py #!/usr/bin/env py ...
- 常用 HTTP 状态码
下面是列举的我在项目中用到过的一些 HTTP 状态码,当然,在具体的使用中并不是用到的状态码越多越好,需要结合自己项目情况来选用适合自己的 HTTP 状态码. HTTP 状态码 含义说明 200 ...