824. Goat Latin - Easy

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.
  • 1 <= S.length <= 150.

time: O(n + k^2), space: O(k)  n: length of string, k: # words in string

complexity analysis: https://leetcode.com/problems/goat-latin/discuss/128368/Java-5-ms-solution-with-time-and-space-complexity-explanation

class Solution {
    public String toGoatLatin(String S) {
        String[] ss = S.split(" ");
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < ss.length; i++) {
            ss[i] = preprocess(ss[i]);
            ss[i] += "ma";
            ss[i] = postprocess(ss[i], i+1);
            sb.append(ss[i] + " ");
        }
        return sb.deleteCharAt(sb.length() - 1).toString();
    }
    private String preprocess(String s) {
        char[] vowels = {‘a‘, ‘e‘, ‘i‘, ‘o‘, ‘u‘, ‘A‘, ‘E‘, ‘I‘, ‘O‘, ‘U‘};

        for(char c : vowels) {
            if(s.charAt(0) == c)
                return s;
        }
        String str = s.substring(1);
        str += s.charAt(0);
        return str;
    }
    private String postprocess(String s, int n) {
        for(int i = 0; i < n; i++)
            s += "a";
        return s;
    }
}

原文地址:https://www.cnblogs.com/fatttcat/p/10134960.html

时间: 2024-10-09 06:18:55

824. Goat Latin - Easy的相关文章

824. 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 La

824. Goat Latin

1 class Solution 2 { 3 public: 4 string toGoatLatin(string S) 5 { 6 S.push_back(' '); //add a space that the loop can deal with the last word 7 string a(150,'a'); // string to add 'a' 8 unordered_set<char> vowel={'a','e','i','o','u','A','E','I','O',

LeetCode 824. Goat Latin (山羊拉丁文)

题目标签:String 首先把vowel letters 保存入 HashSet. 然后把S 拆分成 各个 word,遍历每一个 word: 当 word 第一个 字母不是 vowel 的时候,把第一个char 加到最后: 然后添加“ma” 和 “a“ 到最后: 添加新的"a": 把新的 word 加入 result,还要记得加入空格. Java Solution: Runtime beats 62.66% 完成日期:10/12/2018 关键词:String 关键点:利用HashSe

LeetCode 题解之Goat Latin

1.问题描述 2.问题分析 将句子拆分成单词,然后放入vector中,对每一个单词做改变,最后连成句子返回. 3.代码 1 string toGoatLatin(string S) { 2 vector<string> words; 3 for(string::iterator it = S.begin() ; it != S.end(); ++it ){ 4 string::iterator it_i = it ; 5 while( it_i != S.end() && is

*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 Go

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

LeetCode--String刷题总结

对于不是对于单个字符进行操作,而是对单词进行操作时,可以选择一些库函数 常用的有: 1. str.split(" ");  按照空格将字符串进行划分,得到字符串数组 2. str.substring(i, j); 得到字符串的子串 [ i, j ) 3. str.contains("s1"); str中是否包含 s1字符串 4. str.charAt(7); str在7位置上的char型字符 5. str.trim(); 去除str两侧的空格 应用:山羊拉丁文Goa

LeetCode-824 划水记录3

给定一个由空格分割单词的句子 S.每个单词只包含大写或小写字母. 我们要将句子转换为 "Goat Latin"(一种类似于 猪拉丁文 - Pig Latin 的虚构语言). 山羊拉丁文的规则如下: 如果单词以元音开头(a, e, i, o, u),在单词后添加"ma".例如,单词"apple"变为"applema". 如果单词以辅音字母开头(即非元音字母),移除第一个字符并将它放到末尾,之后再添加"ma".

【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)

Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranking: 440/2797.这次题目似乎比较简单,因为我比赛的时候前三题全做出来了(1:12:39),然后第四题有思路,正在写,没写完,比赛完了写完提交也对了. p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [821]