当前位置:网站首页>力扣解法汇总824-山羊拉丁文
力扣解法汇总824-山羊拉丁文
2022-06-12 02:04:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
给你一个由若干单词组成的句子 sentence ,单词间由空格分隔。每个单词仅由大写和小写英文字母组成。
请你将句子转换为 “山羊拉丁文(Goat Latin)”(一种类似于 猪拉丁文 - Pig Latin 的虚构语言)。山羊拉丁文的规则如下:
如果单词以元音开头('a', 'e', 'i', 'o', 'u'),在单词后添加"ma"。
例如,单词 "apple" 变为 "applema" 。
如果单词以辅音字母开头(即,非元音字母),移除第一个字符并将它放到末尾,之后再添加"ma"。
例如,单词 "goat" 变为 "oatgma" 。
根据单词在句子中的索引,在单词最后添加与索引相同数量的字母'a',索引从 1 开始。
例如,在第一个单词后添加 "a" ,在第二个单词后添加 "aa" ,以此类推。
返回将 sentence 转换为山羊拉丁文后的句子。
示例 1:
输入:sentence = "I speak Goat Latin"
输出:"Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
示例 2:
输入:sentence = "The quick brown fox jumped over the lazy dog"
输出:"heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
提示:
1 <= sentence.length <= 150
sentence 由英文字母和空格组成
sentence 不含前导或尾随空格
sentence 中的所有单词由单个空格分隔
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/goat-latin
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 分割字符串,然后按照规则拼接就好
代码:
public class Solution824 {
public String toGoatLatin(String sentence) {
String[] strs = sentence.split(" ");
StringBuilder builder = new StringBuilder();
StringBuilder aBuilder = new StringBuilder();
for (int i = 0; i < strs.length; i++) {
String str = strs[i];
aBuilder.append("a");
builder.append(transform(str, aBuilder.toString()));
if (i < strs.length-1) {
builder.append(" ");
}
}
return builder.toString();
}
public String transform(String str, String aStr) {
String lowerCase = str.toLowerCase();
if (lowerCase.startsWith("a") || lowerCase.startsWith("e") || lowerCase.startsWith("i") || lowerCase.startsWith("o") || lowerCase.startsWith("u")) {
return str + "ma" + aStr;
}
return str.substring(1) + str.substring(0, 1) + "ma" + aStr;
}
}边栏推荐
- Why do we use Google search ads?
- C language programming classic games - minesweeping
- php安全开发 12博客系统的 系统模块信息的修改
- 颠倒字符串中的单词(split、双端队列)
- UE4\UE5触摸屏touch事件:单指、双指
- Manually tear the linked list (insert, delete, sort) and pointer operation
- Educational knowledge and ability test questions of primary and secondary school educational endowment examination in the second half of 2019 (middle school) - subjective questions
- 括号生成(回溯)
- LeetCode LCP 07. Deliver information
- 商城开发知识点
猜你喜欢

How to automatically color cells in Excel

pip运行报错:Fatal error in launcher: Unable to create process using

为什么我们要使用谷歌搜索广告?

MySQL高级部分知识点

Linux(CentOS6)安装MySQL5.5版本数据库

How to maximize the use of various matching methods—— Google SEM

括号生成(回溯)

MySQL表常用操作思维导图

SQL calculates KS, AUC, IV, psi and other risk control model indicators

On the night of the joint commissioning, I beat up my colleagues
随机推荐
Design principle [Demeter's Law]
adb命令 利用jks文件给apk签名
自适应搜索广告有哪些优势?
ACL 2022 | 预训练语言模型和图文模型的强强联合
C asynchronous programming from simple to deep (III) details awaiter
LeetCode LCP 07. Deliver information
消防栓监测系统毕业设计---论文(附加最全面的从硬件电路设计->驱动程序设计->阿里云物联网搭建->安卓APP设计)
In Net platform using reflectiondynamicobject to optimize reflection calling code
android html5页面加载缓存优化
serialization and deserialization
力扣解法汇总1037-有效的回旋镖
Linux(CentOS7)安裝MySQL-5.7版本
Ozzanation - système d'action basé sur sse
Glfwpollevents() program crash
Software engineering course: Chapter 2 software problem definition and feasibility analysis after class exercises
php安全开发 12博客系统的 系统模块信息的修改
Basic use of MATLAB
力扣解法汇总497-非重叠矩形中的随机点
Ozzanmation action system based on SSE
Lua function