zoukankan      html  css  js  c++  java
  • [LeetCode] 824. Goat Latin

    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.

    山羊拉丁文。

    给定一个由空格分割单词的句子 S。每个单词只包含大写或小写字母。

    我们要将句子转换为 “Goat Latin”(一种类似于 猪拉丁文 - Pig Latin 的虚构语言)。

    山羊拉丁文的规则如下:

    如果单词以元音开头(a, e, i, o, u),在单词后添加"ma"。
    例如,单词"apple"变为"applema"。

    如果单词以辅音字母开头(即非元音字母),移除第一个字符并将它放到末尾,之后再添加"ma"。
    例如,单词"goat"变为"oatgma"。

    根据单词在句子中的索引,在单词最后添加与索引相同数量的字母'a',索引从1开始。
    例如,在第一个单词后添加"a",在第二个单词后添加"aa",以此类推。
    返回将 S 转换为山羊拉丁文后的句子。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/goat-latin
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这个题不难,按照规则做就行了。如果面试遇到这种题,不能心急,耐心把条件看完再动手。更多解释可参考代码注释。

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public String toGoatLatin(String sentence) {
     3         HashSet<Character> vowels = new HashSet<>();
     4         for (char c : "aeiouAEIOU".toCharArray()) {
     5             vowels.add(c);
     6         }
     7 
     8         StringBuilder res = new StringBuilder();
     9         int count = 0;
    10         for (String cur : sentence.split(" ")) {
    11             count++;
    12             if (count > 1) {
    13                 res.append(" ");
    14             }
    15             // 元音字母开头,没有变化
    16             // 不是元音字母开头,则把第一个字母放到最后
    17             if (vowels.contains(cur.charAt(0))) {
    18                 res.append(cur);
    19             } else {
    20                 res.append(cur.substring(1) + cur.charAt(0));
    21             }
    22             // 任何情况都要加ma
    23             res.append("ma");
    24             // 每多一个单词,就在当前单词最后多attach一个a
    25             for (int j = 0; j < count; j++) {
    26                 res.append("a");
    27             }
    28         }
    29         return res.toString();
    30     }
    31 }

    LeetCode 题目总结

  • 相关阅读:
    LIBSVM使用介绍
    Symbian开发平台的搭建之VC++6.0&&Carbide C++ 2.0
    traits:Traits技术初探
    SDK与IDE的选择(附上设置默认SDK)
    浅析COM的思想及原理
    Windows Live Writer 支持的博客
    JQuery笔记(四) 通用选择的尝试
    JQuery笔记(一)
    JQuery笔记(二) animate支持的属性
    在DW绿化版或者精简版中使用扩展管理
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13532854.html
Copyright © 2011-2022 走看看