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. 一次内核 crash 的排查记录

    一次内核 crash 的排查记录 使用的发行版本是 CentOS,内核版本是 3.10.0,在正常运行的情况下内核发生了崩溃,还好有 vmcore 生成. 准备排查环境 crash 内核调试信息rpm ...

  2. LRU 的C# 实现

    首先 先写点儿感悟吧: 本来计划是 晚上回家写的  后来发现还是没坚持的了  上午花了一个多小时  做了一下这个题目  应该还有提高的空间 的,这个题目是在力扣里面看到的  为什么看到这个题目 是因为 ...

  3. Docker知识点整理

    目录 1. Docker简介 1.1 Docker是什么 1.2 在隔离的容器中运行软件 1.3 分发容器 2. Docker镜像 2.1 Docker镜像简介 2.2 Docker镜像常见操作 2. ...

  4. python学习(11)文件的读写操作

    1.读文件的7种操作模式 操作模式 具体含义 'r' 读取 (默认) 'w' 写入(会先截断之前的内容) 'x' 写入,如果文件已经存在会产生异常 'a' 追加,将内容写入到已有文件的末尾 'b' 二 ...

  5. 正则表达式在java中的用法

    /** * 测试正则表达式的基本用法 Pattern 和 Matcher * @author 小帆敲代码 * */public class Demo01 {  public static void m ...

  6. Docker容器映射到宿主机只有tcp6没有tcp问题

    问题描述: Docker容器映射到宿主机后,查询端口连接只有tcp6没有tcp,通过ipv4地址连接时无法连接成功. 处理方法: 1.检查是否开启ipv4端口转发 sysctl net.ipv4.ip ...

  7. 学习ASP.NET Core(06)-Restful与WebAPI

    上一篇我们使用Swagger添加了接口文档,使用Jwt完成了授权,本章我们简答介绍一下RESTful风格的WebAPI开发过程中涉及到的一些知识点,并完善一下尚未完成的功能 .NET下的WebAPI是 ...

  8. 2.4 Go与包

    1.1Go与包 1.1.1.  Go与包 1)开发中,往往要在不同的文件中调用其他文件的函数 2)Go代码最小粒度单位是"包" 3)Go的每一个文件都属于一个包,通过package ...

  9. 06.drf(django)的权限

    默认配置已经启用权限控制 settings 'django.contrib.auth', 默认 migrate 会给每个模型赋予4个权限,如果 ORM 类不托管给django管理,而是直接在数据库中建 ...

  10. 解决el-tree横向滚动条问题

    代码如下 效果如图 仅做下记录,不做过多解释