zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    laravel-admin 关闭debug模式导致异常信息到页面的排查
    laravel-sql
    laravel任务调度出现僵尸进程
    PHP获取首字母笔记
    IP库笔记
    深入理解 js 闭包
    用键盘实现上下选择
    密码保护
    评分效果
    数组去重
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12014584.html
Copyright © 2011-2022 走看看