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. Spring 循环引用(三)源码深入分析版

    @ 目录 前言 正文 分析 doGetBean 为什么Prototype不可以 createBean doCreateBean getEarlyBeanReference getSingleton b ...

  2. 【Spark】如何用Spark查询IP地址?

    文章目录 需求 思路 ip地址转换为Long类型的两种方法 ip地址转换数字地址的原理 第一种方法 第二种方法 步骤 一.在mysql创建数据库表 二.开发代码 需求 日常生活中,当我们打开地图时,会 ...

  3. 超简单笔记本改造nas--一个萌新的摸爬滚打

    最近好久没更新,你们有没有想我啊(手动滑稽)咳咳,言归正传,如同标题,最近闲来无事,打算利用家里的闲置笔记本电脑搭建一个nas.**注意:本文不涉及群晖以及相关专业NAS服务供应商!!!**nas分两 ...

  4. PHP EOF使用说明

    PHP EOF(heredoc) 使用说明 PHP EOF(heredoc)是一种在命令行shell(如sh.csh.ksh.bash.PowerShell和zsh)和程序语言(像Perl.PHP.P ...

  5. 关于Nginx参数路径问题的问题

    本文转载自:https://pureage.info/2013/10/31/130.html 由于工作需要,开始分析nginx的proxy模块,在分析之前,当然要先会用了.于是开始熟悉该模块的一些指令 ...

  6. 解决 Loaded plugins: fastestmirror

    其大概意思是fastestmirror不能使用,fastestmirror是yum的一个加速插件,具体我也没有仔细了解过,可能是系统不支持或者缺少组建导致的.处理办法就是禁用这个插件,方法如下:roo ...

  7. Redux:with React(一)

    作者数次强调,redux和React没有关系(明明当初就是为了管理react的state才弄出来的吧),它可以和其他插件如 Angular, Ember, jQuery一起使用.好啦好啦知道啦.Red ...

  8. RAID0、RAID1及RAID5的区别详解

    目前已有的RAID(Redundant Array of Independent Disks,独立冗余磁盘阵列)技术有很多种,但是RAID0.RAID1.RAID5是最常见的几种方案. 1 RAID0 ...

  9. MYSQL LOCK IN SHARE MODE&FOR UPDATE

    SELECT ... LOCK IN SHARE MODE sets a shared mode lock on the rows read. A shared mode lock enables o ...

  10. Json转化与ExtJS树(后台处理)

    一.JSON对格式化数据的操作: 1.导入依赖包: import org.json.JSONArray; import org.json.JSONException; import org.json. ...