Vowel Mirror

EasyTwo Pointers

Problem

A word-puzzle generator scrambles a phrase by reversing the order of its vowels while every other character stays exactly where it is. The vowels are a, e, i, o, u in either case, and each vowel keeps its own case as it moves.

Given s, return the scrambled string.

Example 1

Input:
s = "programming"
Output:
"prigrammong"

The vowels o, a, i are reversed to i, a, o; consonants stay put.

Example 2

Input:
s = "Data Engine"
Output:
"Deti Engana"

Example 3

Input:
s = "xyz"
Output:
"xyz"

No vowels, nothing moves.

Constraints

  • 0 ≤ s.length ≤ 3·10^5
  • s consists of printable ASCII characters

Hint

Show the first hint

The first vowel must swap with the last vowel, the second with the second-to-last, and so on.

Walkthrough

How to approach it

01Clarify before coding

  • Is 'y' a vowel? No, only a, e, i, o, u.
  • Does case matter? Both cases are vowels, and a vowel keeps its own case when it moves.
  • Must spaces and punctuation stay put? Yes, only vowels move.

02Brute force

Collect all vowels into a list, reverse it, then rebuild the string, taking the next reversed vowel at every vowel position.

Cost: O(n) time and O(n) extra space for the vowel list plus the output; fine, but two pointers do it in one pass with no extra list.

The rest of the walkthrough is in the app

The full walkthrough (key insight, step-by-step, dry run, complexity, edge cases, pitfalls, follow-ups) and solutions in Python and JavaScript are in the app, next to an editor and a terminal that runs your code against the tests.

This problem is part of Pro.