Simple C++ using iterators.


  • 0
    S
    class Solution {
    public:
        string reverseVowels(string s) {
            auto it_fwd = s.begin();
            auto it_bwd = s.end();
            while (it_fwd < it_bwd){
                while(!isVowel(*it_fwd) && it_fwd < it_bwd){
                    ++it_fwd;
                }
                while(!isVowel(*it_bwd) && it_fwd < it_bwd){
                    --it_bwd;
                }
                swap ((*it_fwd), (*it_bwd));
                ++it_fwd;
                --it_bwd;
            }
            return s;
        }
        
        bool isVowel(char ch){
            return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
                || (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') ;
        }
    };
    

Log in to reply
 

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