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();
        }
    }
    
  • 相关阅读:
    使用iptables禁止外网访问tomcat的8080端口
    解决ie浏览器下载apk或ipa变为zip
    'ᄈ'
    centos上安装grafana
    vertica单节点故障恢复 Startup Failed, ASR Required
    vertica审计日志
    centos安装nginx1.17.9
    centos上nginx转发tcp请求
    centos 安装mysql8.0.16
    centos下导出docx为html
  • 原文地址:https://www.cnblogs.com/mapoos/p/13535784.html
Copyright © 2011-2022 走看看