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 ...
随机推荐
- 【杂谈】Disruptor——RingBuffer问题整理(一)
纯CAS为啥比加锁要快? 同样是修改数据,一个采用加锁的方式保证原子性,一个采用CAS的方式保证原子性. 都是能够达到目的的,但是常用的锁(例如显式的Lock和隐式的synchonized),都会把获 ...
- 《Vue.js 2.x实践指南》 已出版
<Vue.js 2.x实践指南>其实在一年前就已经完稿了,只是由于疫情的缘故耽搁了很久才下厂印刷.阅读本书需要具备HTML.CSS和JS基础,本书针对的用户群体主要是:想要快速学习vue技 ...
- Centos7 网卡桥接
一.在centos7主机创建用于虚拟化的网桥 1)增加 /etc/sysconfig/network-scripts/ifcfg-br0 DEVICE=br0BOOTPROTO=staticONBO ...
- [zoj3627]模拟吧
思路:情况只可能是2种,两个人一直向一边走,或者有一个人折回来,对于后一种,枚举折回来的位置就行了.不过要注意两个方向都要处理下. #pragma comment(linker, "/STA ...
- search(13)- elastic4s-histograms:聚合直方图
在聚合的分组统计中我们会面临两种分组元素类型:连续型如时间,自然数等.离散型如地点.产品等.离散型数据本身就代表不同的组别,但连续型数据则需要手工按等长间隔进行切分了.下面是一个按价钱段聚合的例子: ...
- Rx-Volley 自己来封装
自从15年接触了RxJava,对函数式编程越发的喜爱.以前Android项目上网络层都是统一的使用Volley,已经对网络请求的回调,多个回调嵌入各种不爽了,趁着年前任务轻松,赶紧的将Volley封装 ...
- 黑马程序员_毕向东_Java基础视频教程——if 语句(单条语句)(随笔)
if 语句(单条语句) 格式(三种) [注意]:如果 if 控制的语句只有一条,则 这个 { } 括号可以不写 if (条件表达式) { 执行语句; } class Test{ public stat ...
- Spring 获取bean
方法一: ApplicationContext ap = new ClassPathXmlApplicationContext("applicationContext.xml"); ...
- bcdedit 替代easybcd 编辑grub
bcdedit这个命令是win7下,继续安装xubuntu时遇到的.当时xubuntu的语言设置为en-us,时区改为HK,可是仍有很多不太适应的地方.于是,删了xubuntu,重装它.同时,grub ...
- PAT 1009 Product of Polynomials (25分) 指数做数组下标,系数做值
题目 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: E ...