2022-11-04 Daily Challenge

Today I have done leetcode's November LeetCoding Challenge with cpp.

November LeetCoding Challenge 4

Description

Reverse Vowels of a String

Given a string s, reverse only all the vowels in the string and return it.

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.

 

Example 1:

Input: s = "hello"
Output: "holle"

Example 2:

Input: s = "leetcode"
Output: "leotcede"

 

Constraints:

  • 1 <= s.length <= 3 * 105
  • s consist of printable ASCII characters.

Solution

auto speedup = [](){
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  return 0;
}();
int vowel[128] = {
  ['A'] = 1,
  ['E'] = 1,
  ['I'] = 1,
  ['O'] = 1,
  ['U'] = 1,
  ['a'] = 1,
  ['e'] = 1,
  ['i'] = 1,
  ['o'] = 1,
  ['u'] = 1
};
class Solution {
public:
  string reverseVowels(string s) {
    int front = 0;
    int back = s.length() - 1;
    while(front < back) {
      while(!vowel[s[front]] && front != back) {
        front += 1;
      }
      while(!vowel[s[back]] && front != back) {
        back -= 1;
      }
      swap(s[front], s[back]);
      front += 1;
      back -= 1;
    }
    return s;
  }
};

// Accepted
// 480/480 cases passed (12 ms)
// Your runtime beats 67.07 % of cpp submissions
// Your memory usage beats 32.73 % of cpp submissions (8 MB)