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) 。

本文由 .Net中文网 原创发布,欢迎大家踊跃转载。

转载请注明本文地址:https://www.byteflying.com/archives/3971

发表评论

登录后才能评论