824. Goat Latin

824. Goat Latin


题目

Leetcode题目

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.

解决

S的长度为n,有m个单词(n > m)。

1.暴力解决
利用istringstream提取S中的每个单词,依次进行加工,得到目标字符串。

  • 时间复杂度:O(n * m)
  • 空间复杂度:O(n)
class Solution {
public:
    string toGoatLatin(string S) {
        string result;
        vector<string> words = getWord(S);
        int n = words.size();
        for (int i = 0; i < n; i++) {
            if (isVowel(words[i][0])) {
                words[i] += "ma";
            } else {
                string temp = words[i].substr(1);
                words[i] = temp + words[i][0] + "ma";
            }
            int count = i + 1;
            while (count--) {
                words[i] += "a";
            }
            if (i == n - 1) {
                result += words[i];
            } else {
                result += words[i] + " ";
            }
        }
        return result;
    }

    vector<string> getWord(string S) {
        vector<string> words;
        istringstream ss(S);
        string temp;
        while (ss >> temp) {
            words.push_back(temp);
        }
        return words;
    }

    bool isVowel(char c) {
        vector<char> letter = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
        unordered_set<char> vowel(letter.begin(), letter.end());
        return vowel.count(c);
    }
};

2.暴力解决的优化
优化方案:利用string库中的函数简化对word处理。主要使用append()函数来避免上面方法中的while循环。

  • 时间复杂度:O(n)
  • 空间复杂度:O(n)
class Solution {
public:
    string toGoatLatin(string S) {
        string result;
        vector<string> words;
        istringstream ss(S);
        string temp;
        while (ss >> temp) {
            words.push_back(temp);
        }
        map<char, int> vowel;
        vowel['a'] = 1;
        vowel['A'] = 1;
        vowel['e'] = 1;
        vowel['E'] = 1;
        vowel['i'] = 1;
        vowel['I'] = 1;
        vowel['o'] = 1;
        vowel['O'] = 1;
        vowel['u'] = 1;
        vowel['U'] = 1;
        int n = words.size();
        for (int i = 0; i < n; i++) {
            if (!vowel[words[i][0]]) {
                char temp = words[i][0];
                words[i].erase(0, 1);
                result += words[i] + temp;
            } else {
                result += words[i];
            }
            result += "ma";
            // 在字符串末尾添加i+1个a
            result.append(i + 1, 'a');
            if (i != n - 1) result += " ";
        }
        return result;
    }
};

3.stringstream快速解决
stringstream可以类似我们平常使用coutcinstringstream>>可以根据string的空格进行分割,<<可以将string拼接起来。
stringstream有函数str()使得stream buffer转为string

  • 时间复杂度:O(m)
  • 空间复杂度:O(n)
class Solution {
public:
    string toGoatLatin(string S) {
        stringstream iss(S), oss;
        string vowels("aeiouAEIOU"), word, a;
        while (iss >> word) {
            a.push_back('a');
            if (vowels.find_first_of(word[0]) != string::npos) {
                oss << ' ' << word << "ma" << a;
            } else {
                oss << ' ' << word.substr(1) << word[0] << "ma" << a;
            }
        }
        return oss.str().substr(1);
    }
};
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值