leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0345.cpp (520B)


      1 class Solution {
      2     bool is_vowel(char c) {
      3         c = tolower(c);
      4         return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
      5     }
      6 
      7   public:
      8     string reverseVowels(string s) {
      9         int i = 0, j = size(s) - 1;
     10         while (i < j) {
     11             while (i < j && !is_vowel(s[i]))
     12                 i++;
     13             while (i < j && !is_vowel(s[j]))
     14                 j--;
     15             if (i >= j) break;
     16             swap(s[i], s[j]);
     17             i++;
     18             j--;
     19         }
     20         return s;
     21     }
     22 };