Tcdian / keep

今天不想做,所以才去做。
MIT License
5 stars 1 forks source link

824. Goat Latin #302

Open Tcdian opened 3 years ago

Tcdian commented 3 years ago

824. Goat Latin

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

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

山羊拉丁文的规则如下:

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

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"

Note

Tcdian commented 3 years ago

Solution

/**
 * @param {string} S
 * @return {string}
 */
var toGoatLatin = function(S) {
    return S.split(' ').map((word, index) => {
        let target;
        if (['a', 'e', 'i', 'o', 'u'].includes(word[0].toLowerCase())) {
            target = word + 'ma';
        } else {
            target = word.slice(1) + word[0] + 'ma';
        }
        for (let i = 0; i < index + 1; i++) {
            target += 'a';
        }
        return target;
    }).join(' ');
};
function toGoatLatin(S: string): string {
    return S.split(' ').map((word, index) => {
        let target: string;
        if (['a', 'e', 'i', 'o', 'u'].includes(word[0].toLowerCase())) {
            target = word + 'ma';
        } else {
            target = word.slice(1) + word[0] + 'ma';
        }
        for (let i = 0; i < index + 1; i++) {
            target += 'a';
        }
        return target;
    }).join(' ');
};