C++ solution with stack and set


  • 0
    M
    class Solution {
    public:
        string reverseVowels(string s) {
            stack<char> myStack;
            set<char> mySet = {'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U'};
            int strSize = s.length();
            for(int i = 0; i < strSize; ++i){
                if(mySet.count(s[i])){
                    myStack.push(s[i]);
                }
            }
            for(int i = 0; i < strSize; ++i){
                if(mySet.count(s[i])){
                    s[i] = myStack.top();
                    myStack.pop();
                }
            }
            return s;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.