824. 山羊拉丁文

给定一个由空格分割单词的句子 S。每个单词只包含大写或小写字母。

我们要将句子转换为 “Goat Latin”(一种类似于 猪拉丁文 - Pig Latin 的虚构语言)。

山羊拉丁文的规则如下:

如果单词以元音开头(a, e, i, o, u),在单词后添加"ma"。

例如,单词"apple"变为"applema"。

如果单词以辅音字母开头(即非元音字母),移除第一个字符并将它放到末尾,之后再添加"ma"。

例如,单词"goat"变为"oatgma"。

根据单词在句子中的索引,在单词最后添加与索引相同数量的字母’a’,索引从1开始。

例如,在第一个单词后添加"a",在第二个单词后添加"aa",以此类推。

返回将 S 转换为山羊拉丁文后的句子。

示例 1:

输入: “I speak Goat Latin”

输出: “Imaa peaksmaaa oatGmaaaa atinLmaaaaa”

示例 2:

输入: “The quick brown fox jumped over the lazy dog”

输出: “heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa”

说明:

S 中仅包含大小写字母和空格。单词间有且仅有一个空格。

1 <= S.length <= 150。

class Solution {
public String toGoatLatin(String S) { String[] words = S.split(" ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < words.length; i++) {
sb.append(makeGoatLatin(words[i], i));
sb.append(' ');
} String ans = sb.toString();
return ans.substring(0, ans.length() - 1);
} private static String makeGoatLatin(String word, int index) { StringBuilder sb = new StringBuilder();
char ch = word.charAt(0);
if (ch == 'a' || ch == 'e' || ch == 'i' ||ch == 'o' ||ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' ||ch == 'O' ||ch == 'U') {
sb.append(word);
} else {
sb.append(word.substring(1));
sb.append(ch);
}
sb.append("ma"); for (int i = 0; i <= index; i++) {
sb.append('a');
} return sb.toString();
}
}

Java实现 LeetCode 824 山羊拉丁文(暴力)的更多相关文章

  1. [LeetCode] Goat Latin 山羊拉丁文

    A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and up ...

  2. Java for LeetCode 060 Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  3. Java for LeetCode 044 Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  4. [Swift]LeetCode824. 山羊拉丁文 | Goat Latin

    A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and up ...

  5. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  9. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

随机推荐

  1. layui里面的layer模块弹窗,强制居中的方法!!!

    每次调用layer弹窗做动态展示的时候,只要内容不固定或者需要二次渲染 比如layui.form.render()进行渲染的时候 由于弹窗已经出来了,只是content部分的结构改变了宽度或者高度,l ...

  2. DotNet:Socket Server 异步套接字服务端实现

    异步服务器套接字示例 From https://msdn.microsoft.com/zh-cn/library/fx6588te(v=vs.110).aspx 下面的示例程序创建接收来自客户端的连接 ...

  3. Quartus II 中参数化模块库(LPM)的使用

    Quartus II  的LPM库所在的目录是\altera\11.0\quartus\libraries\megafunctions 现以LPM_MULT为例进行演示: 思路:1.首先创建一个pro ...

  4. 日志系列1——slf4j日志框架原理

    目录 1.前言 2.日志门面 3.日志库 4.日志适配器 5.日志库的选用 6.logback.xml 配置文件 1.前言 ​ 说到日志工具,日常工作或学习中肯定听过这些名词:log4j.logbac ...

  5. 一篇文章教会你利用Python网络爬虫获取电影天堂视频下载链接

    [一.项目背景] 相信大家都有一种头疼的体验,要下载电影特别费劲,对吧?要一部一部的下载,而且不能直观的知道最近电影更新的状态. 今天小编以电影天堂为例,带大家更直观的去看自己喜欢的电影,并且下载下来 ...

  6. 2018-06-18 sublime代码编辑器

    sublime代码编辑器:主流前端开发编辑器,体积较小,运行速度快,文本功能强大,支持VI扩展,linux下面有个自带的文本编辑神器,名字叫做:vi ,熟练vi模式可以大量减少使用鼠标的次数,从而增加 ...

  7. spark机器学习从0到1聚类算法 (十)

      一.概念 1.1.定义 按照某一个特定的标准(比如距离),把一个数据集分割成不同的类或簇,使得同一个簇内的数据对象的相似性尽可能大,同时不再同一个簇内的数据对象的差异性也尽可能的大. 聚类属于典型 ...

  8. Navicat for MySQL数据库管理工具安装和破解

    Navicat for MySQL官方下载地址:https://www.navicat.com/en/download/navicat-for-mysql 1.下载后安装 navicat110_mys ...

  9. PART(Persistent Adaptive Radix Tree)的Java实现源码剖析

    论文地址 Adaptive Radix Tree: https://db.in.tum.de/~leis/papers/ART.pdf Persistent Adaptive Radix Tree: ...

  10. Django之from.Form内置字段

    from django import forms Field required=True, 是否允许为空 widget=None, HTML插件 label=None, 用于生成Label标签或显示内 ...