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 }
  • 相关阅读:
    fms服务器端呼叫客户端
    Linux C取整的方法
    fms客户端呼叫服务器端
    Tree Control DataProviders
    IE7下的css style display的兼容处理
    Maxthon,TheWorld,MyIE等多标签浏览器的Flash缓存问题
    Android深入浅出系列之实例应用—弹出消息Toast对象的使用纯文本方式(一)
    C#温故而知新学习系列之XML编程—Xml读取器XmlReader类(二)
    一步一个脚印学习WCF系列之WCF概要—生成元数据与代理(五)
    Eclipse 常用快捷键(转)
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12014584.html
Copyright © 2011-2022 走看看