Reverse only the vowels (a, e, i, o, u, both lowercase and uppercase) in a string, leaving all other characters in their original positions. Return null for null input and empty string for empty input.

Examples:

Input: str = 'hello' Output: 'holle' Explanation: The vowels e and o are swapped Input: str = 'leetcode' Output: 'leotcede' Explanation: The vowels e, e, o, e are reversed to o, e, e, e in place of the original vowel positions Input: str = 'aA' Output: 'Aa' Explanation: Both characters are vowels, so they are swapped Input: str = null Output: null Explanation: Null input returns null Input: str = '' Output: '' Explanation: Empty string returns empty string
Apex Code Editor

Welcome to Lightning Challenge!

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay September 2026 (Sep 16–30)Sponsored by FLXBL

Contest runs September 16 - 30. Complete challenges to climb the leaderboard!

Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes

Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In