Java实现 LeetCode 824 山羊拉丁文(暴力)
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 山羊拉丁文(暴力)的更多相关文章
- [LeetCode] Goat Latin 山羊拉丁文
A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and up ...
- 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 ...
- Java for LeetCode 044 Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- [Swift]LeetCode824. 山羊拉丁文 | Goat Latin
A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and up ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- for do-while while区别
分别用for do-while while求1-100的和
- 【poj 2406】Power Strings 后缀数组DC3模板 【连续重复子串】
Power Strings 题意 给出一个字符串s,求s最多由几个相同的字符串重复而成(最小循环节的重复次数) 思路 之前学习KMP的时候做过. 我的思路是:枚举字符串的长度,对于当前长度k,判断\( ...
- js移动端复制到剪贴板
// 复制到剪切板 function copy(str){ var save = function (e){ e.clipboardData.setData('text/plain',str);//c ...
- springMVC的自定义annotation(@Retention@Target)详解
自定义注解: 使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节.在定义注解时,不能继承其他的注解或接口.@ ...
- XSS挑战之旅(通过看代码解题)
XSS 挑战之旅 level 1 没有什么过滤 payload: <script>alert(1)</script> level 2 php关键代码: echo "& ...
- LeetCode链表专题
链表 套路总结 1.多个指针 移动 2.虚假链表头:凡是有可能删除头节点的都创建一个虚拟头节点,代码可以少一些判断(需要用到首部前一个元素的时候就加虚拟头指针) 3.快慢指针 如leetcode160 ...
- masonry中的make,remake,update
- (void)viewDidLoad { [super viewDidLoad]; self.navigationController.navigationBar.translucent = NO; ...
- PHP中面向对象特性实现
PHP近些年来成为全球最流行的网页编程语言,该语言以弱类型.易兼容.门槛低.开发快.功能强著称,且听别人这么说,我在有了c和c#基础后学习PHP过程中也并不是很顺利,该语言的一些特殊的语法规则又是让我 ...
- 用一个python文件去调用另一个python文件,关于相对路径的处理?
比如用a.py调用执行b.py,但是a.py和b.py路径环境不一样,而b.py中有使用相对路径读取文件,这时会报错,怎么在a.py中进行配置,使其调用b.py时路径和其相同,能否做到? 比如目录结构 ...
- 「雕爷学编程」Arduino动手做(20)—水银开关模块
37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里 ...