1. 程式人生 > >C#LeetCode刷題之#824-山羊拉丁文(Goat Latin)

C#LeetCode刷題之#824-山羊拉丁文(Goat Latin)

問題

給定一個由空格分割單詞的句子 S。每個單詞只包含大寫或小寫字母。

我們要將句子轉換為 “Goat Latin”(一種類似於 豬拉丁文 - Pig Latin 的虛構語言)。

山羊拉丁文的規則如下:

如果單詞以母音開頭(a, e, i, o, u),在單詞後新增"ma"。
例如,單詞"apple"變為"applema"。

如果單詞以子音字母開頭(即非母音字母),移除第一個字元並將它放到末尾,之後再新增"ma"。
例如,單詞"goat"變為"oatgma"。

根據單詞在句子中的索引,在單詞最後新增與索引相同數量的字母'a',索引從1開始。
例如,在第一個單詞後新增"a",在第二個單詞後新增"aa",以此類推。
返回將 S 轉換為山羊拉丁文後的句子。

輸入: "I speak Goat Latin"

輸出: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"

輸入: "The quick brown fox jumped over the lazy dog"

輸出: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"

說明:

S 中僅包含大小寫字母和空格。單詞間有且僅有一個空格。
1 <= S.length <= 150。


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. 

Input: "I speak Goat Latin"

Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"

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.

示例

public class Program {

    public static void Main(string[] args) {
        var S = "I speak Goat Latin";

        var res = ToGoatLatin(S);
        Console.WriteLine(res);

        Console.ReadKey();
    }

    private static string ToGoatLatin(string S) {
        //按空格分隔
        var split = S.Split(' ');
        //定義結果
        var res = string.Empty;
        for(var i = 0; i < split.Length; i++) {
            if(IsStartsWithVowel(split[i])) {
                res += split[i];
            } else {
                //子音字母開頭時,首字母后置
                res += split[i].Substring(1) + split[i][0];
            }
            //追回字串 ma 和按索引重複的字元 a
            res += "ma" + RepeatString(i + 1) + " ";
        }
        return res.Trim();
    }

    private static bool IsStartsWithVowel(string word) {
        //判斷是不是母音字母
        var s = word[0].ToString().ToLower();
        return s == "a" || s == "e" || s == "i" || s == "o" || s == "u";
    }

    private static string RepeatString(int count) {
        //重複字串
        var c = 'a';
        var res = string.Empty;
        for(var i = 0; i < count; i++) {
            res += c;
        }
        return res;
    }

}

以上給出1種演算法實現,以下是這個案例的輸出結果:

Imaa peaksmaaa oatGmaaaa atinLmaaaaa

分析:

顯而易見,以上演算法的時間複雜度為: O(n) 。