• LeetCode 824. Goat Latin


    原题链接在这里:https://leetcode.com/problems/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.

    题解:

    For each word, if beginning char is not within vows set, move its first char to the last position.

    Other logic remains the same.

    Time Complexity: O(n). n = S.length().

    Space: O(n).

    AC Java:

     1 class Solution {
     2     public String toGoatLatin(String S) {
     3         if(S == null || S.length() == 0){
     4             return S;
     5         }
     6         
     7         HashSet<Character> vows = new HashSet<>();
     8         for(char c : "aeiouAEIOU".toCharArray()){
     9             vows.add(c);
    10         }
    11         
    12         StringBuilder sb = new StringBuilder();
    13         String [] words = S.trim().split("\s+");
    14         for(int i = 0; i<words.length; i++){
    15             String word = words[i];
    16             char first = word.charAt(0);
    17             if(!vows.contains(first)){
    18                 word = word.substring(1)+first;
    19             }
    20             
    21             word = word + "ma";
    22             for(int k = 0; k<=i; k++){
    23                 word = word + "a";
    24             }
    25             
    26             sb.append(word + " ");
    27         }
    28         
    29         sb.deleteCharAt(sb.length() - 1);
    30         return sb.toString();
    31     }
    32 }
  • 相关阅读:
    (转)使用BigDecimal进行精确运算
    date——sql查询
    (转)每天一个linux命令(8):cp 命令,复制文件和文件夹
    (转)每天一个linux命令(15):tail 命令
    (转)Linux 下 查看以及修改文件权限
    (转)用JUnit4进行单元测试
    (转)Spring Boot Junit单元测试
    (转)ZXing解析二维码
    (转)ZXing生成二维码和带logo的二维码,模仿微信生成二维码效果
    (转)js jquery.qrcode生成二维码 带logo 支持中文
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12014584.html
Copyright © 2020-2023  润新知