作者: 负雪明烛
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:

  1. Input: "I speak Goat Latin"
  2. Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"

Example 2:

  1. Input: "The quick brown fox jumped over the lazy dog"
  2. Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"

Notes:

  1. S contains only uppercase, lowercase and spaces. Exactly one space between each word.
  2. 1 <= S.length <= 150.

题目大意

要把英语翻译成Goat Latin语。翻译规则如下:

把英文句子按照空格分割成各个单词。

  1. 如果单词以元音字母开头,后面接上"ma"
  2. 如果不以元音开头,把首字母移到最后,然后接上"ma"
  3. 每个单词后面接上在句子中出现的第几位置个"a"

解题方法

python天生的适合处理字符串问题。这种纯粹的拼操作的,没有思考的题目,分分钟搞定啊~

太简单了,不讲了。

  1. class Solution:
  2. def toGoatLatin(self, S):
  3. """
  4. :type S: str
  5. :rtype: str
  6. """
  7. vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
  8. words = S.split(' ')
  9. new_words = []
  10. for i, word in enumerate(words):
  11. if word[0] in vowels:
  12. word += 'ma'
  13. else:
  14. word = word[1:] + word[0] + 'ma'
  15. word += 'a' * (i + 1)
  16. new_words.append(word)
  17. return ' '.join(new_words)

日期

2018 年 5 月 27 日 —— 周末的天气很好~
2018 年 11 月 9 日 —— 睡眠可以

【LeetCode】824. Goat Latin 解题报告(Python)的更多相关文章

  1. LeetCode 824 Goat Latin 解题报告

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

  2. [LeetCode] 824. Goat Latin

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

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

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

  4. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  5. 824. Goat Latin - LeetCode

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

  6. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  7. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  8. 【Leetcode_easy】824. Goat Latin

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

  9. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

随机推荐

  1. EXCEL ctrl+e 百变用法不只是你用的那么简单

    Excel2013版本中,新增加了一个快捷键:Ctrl+E,可以依据字符之间的关系,实现快速填充功能.一些需要使用公式或者其他功能进行解决的问题,现在只要一个快捷键就可以实现了. 用法1:快速拆解出需 ...

  2. C#表格GridView显示2位百分比

    <asp:BoundField HeaderText="占比" DataField="number" DataFormatString="{0: ...

  3. cvc-complex-type.2.3: Element 'servlet' cannot have character [children], because the type's content

    错误原因:粘贴代码 <servlet> <servlet-name>barServlet</servlet-name> <servlet-class>S ...

  4. 强化学习实战 | 表格型Q-Learning玩井字棋(二)

    在 强化学习实战 | 表格型Q-Learning玩井字棋(一)中,我们构建了以Game() 和 Agent() 类为基础的框架,本篇我们要让agent不断对弈,维护Q表格,提升棋力.那么我们先来盘算一 ...

  5. windows下 apache 二级域名相关配置 【转】

    转至: http://www.th7.cn/Program/php/201306/141305.shtml 今天给大家总结下 windows 下 apache的二级域名的相关配置 下面就利用本地127 ...

  6. 【MPI环境配置】 vs2019配置MPI环境

    MPI 即 Message-Passing Interface,提供了一系列并行编程的接口,为了在本机能够学习和使用并行编程,需要提前安装MPI; 配置环境: Microsoft Visual Stu ...

  7. Default Assignment Operator and References

    We have discussed assignment operator overloading for dynamically allocated resources here . This is ...

  8. springMVC中响应的返回值获取方式

    package com.hope.controller;import com.hope.domain.User;import org.springframework.stereotype.Contro ...

  9. 【C++】使用VS2022开发可以在线远程编译部署的C++程序

    前言: 今天没有前言. 一.先来一点C++的资源分享,意思一下. 1.c++类库源码以及其他有关资源.站点是英文的,英文不好的话可以谷歌浏览器在线翻译.http://www.cplusplus.com ...

  10. mysql的事务详解

    事务及其ACID属性 事务是由一组SQL语句组成的逻辑处理单元,事务具有以下4个属性,通常简称为事务的ACID属性. 原子性(Atomicity) :事务是一个原子操作单元,其对数据的修改,要么全都执 ...