*LeetCode--Goat Latin

Posted SkyeAngel

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了*LeetCode--Goat Latin相关的知识,希望对你有一定的参考价值。

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.

 

自己的解法:

就是对整个字符串的每一个字符利用空格进行判断,得出一个一个单独的单词,然后在对含有元音的单词和辅音的单词进行各自独立的单词,虽然做出来了,但是效果不好,很笨重,看到别人使用很多字符串现有的方法,contains(), charAt(), 这些我也使用了,但是像 trim()  split()  substring()  就没想到,就是自己不会向那方面去想,但是看到别人的方法,就  哇!  好巧妙~~的感觉。

看来还是刷题刷少了,继续努力~_~

class Solution {
    public String toGoatLatin(String S) {
        if(S == null || S.length() == 0) return "a";
        StringBuilder str = new StringBuilder();
        int i = 0;
        String s = "ma";
        while(i < S.length()){
            s += "a";
            boolean flag = false;
            if(S.charAt(i) == ‘a‘ || S.charAt(i) == ‘e‘ 
               || S.charAt(i) == ‘i‘ || S.charAt(i) == ‘o‘ 
               ||S.charAt(i) == ‘u‘ || S.charAt(i) == ‘A‘ || S.charAt(i) == ‘E‘ 
               || S.charAt(i) == ‘I‘ || S.charAt(i) == ‘O‘ 
               ||S.charAt(i) == ‘U‘)
                flag = true;
            if(flag){
                while(i < S.length() && ‘ ‘ != S.charAt(i)){
                    str.append(S.charAt(i));
                    i++;
                }
                str.append(s);
            } else{
                char ch = S.charAt(i++);
                while(i < S.length() && ‘ ‘ != S.charAt(i)){
                    str.append(S.charAt(i));
                    i++;
                }
                str.append(ch).append(s);
            }
            if(i != S.length()) str.append(‘ ‘);
            i++;
        }
        return str.toString();
    }
}

  

别人的解法:先放上,等过一段时间再来重新做一遍,争取用到很好的方法(起码不是自己那么笨的方法)

class Solution {
    public String toGoatLatin(String S) {
        String vowel = "aeiouAEIOU";
        String result="";
        String addA = "a";
        for (String word:S.split(" ")){
            if (vowel.contains(word.charAt(0)+"")){
                result+=word+"ma"+addA+" ";
            }
            else {
                result+=word.substring(1,word.length())+word.charAt(0)+"ma"+addA+" ";
            }
            addA+="a";
        }
        return result.trim();
    }
}

  

 






以上是关于*LeetCode--Goat Latin的主要内容,如果未能解决你的问题,请参考以下文章

[已解决]报错:UnicodeEncodeError: 'latin-1' codec can't encode characters in position 80-81(代码

python mysql utf-8 latin

latin-1 codec cant encode characters in position 42-48: ordinal not in range256 下载文件时候报错

在 C++ 中标记 latin-1 文本

php 此代码段用于以下知识库文章 - https://kb.wpbeaverbuilder.com/article/637-add-latin-extended-capabilitiesfo

LeetCode 题解之Goat Latin