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 ...
随机推荐
- band-pass filtering
from scipy import signal # 带通滤波器 # 信号,频率下限,频率上限, 采样率 def bandPass(signals, fre_low, fre_high, fs): b ...
- Openwrt:基于MT7628/MT7688的PWM驱动
前言 MT7628/MT7688的PWM驱动相关资料较少,官方的datasheet基本也是一堆寄存器,啃了许久,终于嚼出了味道.由于PWM存在IO口复用的问题,所以要提前配置好GPIO的工作方式,不然 ...
- javaweb学习之路(3)Cookie
1.Cookies的原理 1)首先浏览器向服务器发出请求. 2)服务器就会根据需要生成一个Cookie对象,并且把数据保存在该对象内. 3)然后把该Cookie对象放在响应头,一并发送回浏览器. 4) ...
- 一站式Kafka平台解决方案——KafkaCenter
KafkaCenter是什么 KafkaCenter是一个针对Kafka的一站式,解决方案.用于Kafka集群的维护与管理,生产者和消费者的监控,以及Kafka部分生态组件的使用. 对于Kafka的平 ...
- Raft翻译
英文原文:https://web.stanford.edu/~ouster/cgi-bin/papers/raft-atc14 In Search of an Understandable Conse ...
- react项目中使用less并修改antd主题样式
一.react项目中使用less 1. 安装配置 npm i -D less less-loader 2. 查看webpack配置 npm run eject 此操作不可逆,生成新的目录. 3. 修改 ...
- webpack指南(四)shimming
shimming 将一个新的 API 引入到一个旧的环境中,而且仅靠旧的环境中已有的手段实现. ProvidePlugin 我们在程序中暴露一个变量,通知webpack某个库被使用,webpack将在 ...
- 8086 8255A proteus仿真实验
目录 实验内容 电路图 电路分析 代码 实验内容 数码管循环显示0123456789abcdef- 电路图 电路分析 端口地址和控制字地址主要看电路图,片选信号由译码器的\(\overline{IO1 ...
- H3C S5500三层交换机划分Vlan与H3C路由组网
基本属性: vlan特性:三层互通,两层隔离.三层交换机不同vlan之间默认是互通的,两次交换机不同vlan是隔离的. vlan IP:就是定义一个vlan下所有机器的网关地址,该vlan下的机器网关 ...
- 基于Netty包中的Recycler实现的对象池技术详解
一.业务背景 当项目中涉及到频繁的对象的创建和回收的时候,就会出现频繁GC的情况,这时就出现了池化的技术来实现对象的循环使用从而避免对象的频繁回收,Netty包下的Recycler就实现了这一功能.当 ...