加载中...
824-山羊拉丁文(Goat Latin)
发表于:2021-12-03 | 分类: 简单
字数统计: 1.1k | 阅读时长: 5分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/goat-latin

英文原文

You are given a string sentence that consist 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.
    <ul>
        <li>For example, the word <code>&quot;apple&quot;</code> becomes <code>&quot;applema&quot;</code>.</li>
    </ul>
    </li>
    <li>If a word begins with a consonant (i.e., not a vowel), remove the first letter and append it to the end, then add <code>&quot;ma&quot;</code>.
    <ul>
        <li>For example, the word <code>&quot;goat&quot;</code> becomes <code>&quot;oatgma&quot;</code>.</li>
    </ul>
    </li>
    <li>Add one letter <code>&#39;a&#39;</code> to the end of each word per its word index in the sentence, starting with <code>1</code>.
    <ul>
        <li>For example, the first word gets <code>&quot;a&quot;</code> added to the end, the second word gets <code>&quot;aa&quot;</code> added to the end, and so on.</li>
    </ul>
    </li>
    

Return the final sentence representing the conversion from sentence to Goat Latin.

 

Example 1:

Input: sentence = "I speak Goat Latin"
Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"

Example 2:

Input: sentence = "The quick brown fox jumped over the lazy dog"
Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"

 

Constraints:

  • 1 <= sentence.length <= 150
  • sentence consists of English letters and spaces.
  • sentence has no leading or trailing spaces.
  • All the words in sentence are separated by a single space.

中文题目

给定一个由空格分割单词的句子 S。每个单词只包含大写或小写字母。

我们要将句子转换为 “Goat Latin”(一种类似于 猪拉丁文 - Pig Latin 的虚构语言)。

山羊拉丁文的规则如下:

  • 如果单词以元音开头(a, e, i, o, u),在单词后添加"ma"
    例如,单词"apple"变为"applema"

  • 如果单词以辅音字母开头(即非元音字母),移除第一个字符并将它放到末尾,之后再添加"ma"
    例如,单词"goat"变为"oatgma"

  • 根据单词在句子中的索引,在单词最后添加与索引相同数量的字母'a',索引从1开始。
    例如,在第一个单词后添加"a",在第二个单词后添加"aa",以此类推。

返回将 S 转换为山羊拉丁文后的句子。

示例 1:

输入: "I speak Goat Latin"
输出: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"

示例 2:

输入: "The quick brown fox jumped over the lazy dog"
输出: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"

说明:

  • S 中仅包含大小写字母和空格。单词间有且仅有一个空格。
  • 1 <= S.length <= 150

通过代码

官方题解

方法 1:字符串

想法

我们直观地解决这个问题,问题的难点在于实现。

算法

对于句子中的每个 word,如果是元音字母,就不变;如果是辅音字母,就旋转这个单词(在 Python 中是 word[1:] + word[:1],在 Java 中是 word.substring(1) + word.substring(0, 1)

然后,我们加入 "ma" 和期望数量的 "a" 以及一个空格。

[]
class Solution { public String toGoatLatin(String S) { Set<Character> vowel = new HashSet(); for (char c: new char[]{'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}) vowel.add(c); int t = 1; StringBuilder ans = new StringBuilder(); for (String word: S.split(" ")) { char first = word.charAt(0); if (vowel.contains(first)) { ans.append(word); } else { ans.append(word.substring(1)); ans.append(word.substring(0, 1)); } ans.append("ma"); for (int i = 0; i < t; i++) ans.append("a"); t++; ans.append(" "); } ans.deleteCharAt(ans.length() - 1); return ans.toString(); } }
[]
class Solution(object): def toGoatLatin(self, S): def convert(word): if word[0] not in 'aeiouAEIOU': word = word[1:] + word[:1] return word + 'ma' return " ".join(convert(word) + 'a' * i for i, word in enumerate(S.split(), 1))

复杂度分析

  • 时间复杂度:$O(N^2)$,其中 $N$ 是 S 的长度。这包含旋转单词的复杂度以及添加额外 "a" 字符。
  • 空间复杂度:$O(N^2)$,空间需要考虑加入的额外字符 "a"

统计信息

通过次数 提交次数 AC比率
17589 28536 61.6%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
823-带因子的二叉树(Binary Trees With Factors)
下一篇:
825-适龄的朋友(Friends Of Appropriate Ages)
本文目录
本文目录