2Brute2Best
πŸ”₯0

Reverse Vowels of a String

EasyΒ·LeetCode #345 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You are given a string s.

Reverse the order of only the vowels in it, and leave every other character exactly where it is. Return the new string.

The vowels are a, e, i, o, u, in lowercase or uppercase.

Input

A string s of printable ASCII characters.

Output

The string with its vowels in reverse order and all other characters unchanged.

Example 1
Input: s = "IceCreAm"
Output: "AceCreIm"

The vowels, in order, are I, e, e, A. Reversed they are A, e, e, I. Put them back into the vowel slots: "AceCreIm".

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

Vowels e, e, o, e become e, o, e, e.

Constraints
  • β€’ 1 ≀ s.length ≀ 3 Β· 10^5
  • β€’ s consists of printable ASCII characters.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œreverse”
β†’ Reversing = swap first with last, then move inward β†’ opposite ends.
β€œonly the vowels”
β†’ Each pointer must skip the characters that don't count, on its own.
β€œeverything else stays where it is”
β†’ Skipped characters never need another look.
03

Approach Complexity Ladder

Brute Force β€” collect the vowels, write them back reversedO(N)O(N)

Copy vowels to a list, then refill the vowel slots from the end of the list

Optimal β€” two pointers that skip consonantsO(N)O(1)

Each side skips non-vowels; swap when both stop on a vowel

Brute Force Intuition

Walk through s and copy every vowel into a separate string. Then walk through s again. Every time you hit a vowel slot, write in the next vowel from the end of the list.

Recognition rule for this sub-pattern:

Start at both ends of a sorted (or symmetric) structure. Each step, move the side that cannot do better.
Remember this Β· Two Pointers
Trigger
Sorted array, pairs/triplets with a target, comparing from both ends, compacting an array in place.
Think
If I compare the elements at two positions, can I safely discard one side?
Key question
Which pointer moves, and why can the skipped elements never be the answer?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Two Pointers translate into production architectures and low-level memory mechanics.