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();
        }
    }
    
  • 相关阅读:
    php configure help
    ubuntu下编译安装PHP
    【转】Ubuntu编译安装mysql源码
    Java 处理异常 9 个最佳实践,你知道几个?
    HashMap工作原理
    LinkedList
    SpringMVC常用注解@Controller,@Service,@repository,@Component
    HTML5 APP应用实现图片上传及拍照上传功能
    bootstrap-datepicker 与bootstrapValidator同时使用时,选择日期后,无法正常触发校验
    js
  • 原文地址:https://www.cnblogs.com/mapoos/p/13535784.html
Copyright © 2011-2022 走看看