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 }
  • 相关阅读:
    JavaScript大杂烩1
    JavaScript大杂烩0
    Scrum敏捷开发沉思录
    C#的变迁史
    C#的变迁史
    C#的变迁史
    巧用浏览器F12调试器定位系统前后端bug-转载
    Jmeter如何把响应数据的结果保存到本地的一个文件
    Jmeter察看结果树的响应数据中的中文显示乱码问题处理
    Jmeter如何将上一个请求的结果作为下一个请求的参数——使用正则表达式提取器转载
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12014584.html
Copyright © 2011-2022 走看看