资讯专栏INFORMATION COLUMN

[LeetCode] 824. Goat Latin

coolpail / 1555人阅读

Problem (and this is a very stupid problem...)

A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only.

We would like to convert the sentence to "Goat Latin" (a made-up language similar to Pig Latin.)

The rules of Goat Latin are as follows:

If a word begins with a vowel (a, e, i, o, or u), append "ma" to the end of the word.
For example, the word "apple" becomes "applema".

If a word begins with a consonant (i.e. not a vowel), remove the first letter and append it to the end, then add "ma".
For example, the word "goat" becomes "oatgma".

Add one letter "a" to the end of each word per its word index in the sentence, starting with 1.
For example, the first word gets "a" added to the end, the second word gets "aa" added to the end and so on.
Return the final sentence representing the conversion from S to Goat Latin.

Example 1:

Input: "I speak Goat Latin"
Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"

Example 2:

Input: "The quick brown fox jumped over the lazy dog"
Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
 

Notes:

S contains only uppercase, lowercase and spaces. Exactly one space between each word.
1 <= S.length <= 150.

Solution
class Solution {
    public String toGoatLatin(String S) {
        String[] words = S.split(" ");
        String as = "";
        StringBuilder sb = new StringBuilder();
        Set vowel = new HashSet<>();
        vowel.add("a");
        vowel.add("e");
        vowel.add("i");
        vowel.add("o");
        vowel.add("u");
        vowel.add("A");
        vowel.add("E");
        vowel.add("I");
        vowel.add("O");
        vowel.add("U");
        for (int i = 0; i < words.length; i++) {
            as += "a";
            char first = words[i].charAt(0);
            if (vowel.contains(first)) {
                sb.append(words[i]+"ma"+as+" ");
            } else {
                sb.append(words[i].substring(1)+first+"ma"+as+" ");
            }
        }
        return sb.toString().trim();
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77266.html

相关文章

  • Leetcode PHP题解--D60 824. Goat Latin

    摘要:题目链接题目分析给定一个句子,由大小写英文字母组成,以空格为单词的分割。即,在第个单词按以上规则转换完成后,再加个。分割后,判断首字母是否不是元音。不是元音,则将第一个字母移到最后。给字符串末尾添加。 D60 824. Goat Latin 题目链接 824. Goat Latin 题目分析 给定一个句子,由大小写英文字母组成,以空格为单词的分割。 按以下规则修改单词: 如果一个单词...

    xorpay 评论0 收藏0
  • 搜索树的思想,以及增删查改的实现

    摘要:目录搜索树的概念查找操作插入操作删除操作改的操作搜索树的概念二叉搜索树又被称为排序树,它或者是一颗空树,或者是一棵具有以下性质的二叉树若它的左子树不为空,则左子树上所有节点的值都小于根节点的值若它的右子树不为空,则右子 ...

    YanceyOfficial 评论0 收藏0
  • export和import的用法总结

    摘要:把直接加到声明前面就可以省略无论怎样输出,输入的时候都需要。其实这种导出方式可以看成是命名导出的变种,只不过把命名写成了。对应输入的例子参考文章详解中与的用法和区别我在 ES6中export一般的用法有两种 命名导出(Named exports) 默认导出(Default exports) 命名导出(Named exports) 就是每一个需要输出的数据类型都要有一个name,统一...

    EasonTyler 评论0 收藏0
  • export和import的用法总结

    摘要:把直接加到声明前面就可以省略无论怎样输出,输入的时候都需要。其实这种导出方式可以看成是命名导出的变种,只不过把命名写成了。对应输入的例子参考文章详解中与的用法和区别我在 ES6中export一般的用法有两种 命名导出(Named exports) 默认导出(Default exports) 命名导出(Named exports) 就是每一个需要输出的数据类型都要有一个name,统一...

    CoderBear 评论0 收藏0
  • ZooKeeper 学习笔记

    摘要:与此同时,小组也一同致力于项目,参与了很多动物命名的项目,其中有广为人知的项目。主控服务器将所有更新操作序列化,利用协议将数据更新请求通知所有从属服务器,保证更新操作。在术语下,节点被称为。命名为的,由系统自动生成,用配额管理。 ZooKeeper 介绍 ZooKeeper(wiki,home,github) 是用于分布式应用的开源的分布式协调服务。通过暴露简单的原语,分布式应用能在之...

    funnyZhang 评论0 收藏0

发表评论

0条评论

coolpail

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<