zoukankan      html  css  js  c++  java
  • 0824. Goat Latin (E)

    Goat Latin (E)

    题目

    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.

    题意

    按照指定格式将字符串中的所有单词进行变换。

    思路

    直接一个一个单词进行处理即可。


    代码实现

    Java

    class Solution {
        private Set<Character> vowels = new HashSet<>() {
            {
                add('a');
                add('A');
                add('e');
                add('E');
                add('i');
                add('I');
                add('o');
                add('O');
                add('u');
                add('U');
            }
        };
    
        public String toGoatLatin(String S) {
            String[] ss = S.split(" ");
            for (int i = 0; i < ss.length; i++) {
                if (vowels.contains(ss[i].charAt(0))) {
                    ss[i] = ss[i] + "ma" + "a".repeat(i + 1);
                } else {
                    ss[i] = ss[i].substring(1) + ss[i].charAt(0) + "ma" + "a".repeat(i + 1);
                }
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < ss.length; i++) {
                if (i > 0) {
                    sb.append(" ");
                }
                sb.append(ss[i]);
            }
            return sb.toString();
        }
    }
    
  • 相关阅读:
    token认证、JWT
    DRF序列化、认证、跨域问题
    JS 作用域 p1
    如何配置图标
    关于批量更新与删除
    windows 公司内部搭建禅道(项目管控)
    JS 创建自定义对象的方式方法
    apicloud 消息推送与接收
    apicloud 自定义模块的开发与上架注意事项
    apicloud 第一篇
  • 原文地址:https://www.cnblogs.com/mapoos/p/13535784.html
Copyright © 2011-2022 走看看