824. Goat Latin

https://leetcode.com/problems/goat-latin/

句子中每个元音开头的单词后面加ma,辅音开头的把首字母放到单词最后并加ma,每个单词再在最后按照它所在的index加相应数目的a。用stringsteam把单词抽出来,再依据题意做拼接。

/*
 * @lc app=leetcode id=824 lang=cpp
 *
 * [824] Goat Latin
 */

// @lc code=start
class Solution {
public:
    string toGoatLatin(string S) {
        string tmp;
        stringstream ss(S), os;
        int i = 1;
        while (getline(ss, tmp, ' ')) {
            if (i != 1) os << " ";
            const auto c = tmp[0];
            if (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U') {
                os << tmp << "ma";
            } else {
                os << tmp.substr(1) << c << "ma";
            } 
            for (int j = 0; j < i; ++j) os << "a";
            ++i;
        } 
        
        return os.str();
    }
};
// @lc code=end

Last updated