LeetCode 824 Goat Latin

LeetCode 824

Goat Latin

  • Problem Description:
    对字符串中的每个单词进行处理,若单词的首字母不是元音字母,则将首字母放在该单词的最后面,如果单词的首字母是元音字母,则不需要进行处理。然后分别在每个单词后加上字符串ma和i个字符a,其中i从1开始递增。

    具体的题目信息:
    https://leetcode.com/problems/goat-latin/description/

  • Example: 题目给出的例子如下所示
  • Solution:
class Solution {
public:
    string toGoatLatin(string S) {
        vector<string> sentence;
        string temp = "";
        int flag = 1;
        for (int i = 0; i < S.length(); i++) {
            if (S[i] != ' '&&i != S.length()-1) {
                temp += S[i];
            } else {
                if (i == S.length()-1) {
                    temp += S[i];
                }
                if(!isVowel(temp[0])) {
                    int j = 1;
                    char t = temp[0];
                    while(j < temp.size()) {
                        temp[j-1] = temp[j];
                        j++;
                    }
                    temp[j-1] = t;
                }
                temp += "ma";
                for (int k = 0; k < flag; k++) {
                    temp += "a";
                }
                sentence.push_back(temp);
                temp = "";
                flag++;
            }
        }
        string result = "";
        result += sentence[0];
        int m = 1;
        while(m < sentence.size()) {
            result += " ";
            result += sentence[m];
            m++;
        }
        return result;
    }
    //判断首字母是否是元音字母
    bool isVowel(char t) {
        if (t == 'A'||t == 'E'||t == 'I'||t == 'O'||t == 'U'||t == 'a'||t == 'e'||t == 'i'||t == 'o'||t == 'u')
            return true;
        return false;
    }
};

你可能感兴趣的:(leetcode,string)