FastPrepReverse Vowels of a String

Reverse Vowels of a String

Google logoGoogle● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given a string s, reverse only its vowel characters and return the resulting string.

The vowels are a, e, i, o, and u, in either lowercase or uppercase. Preserve every vowel's original character and case while reversing their order. Every non-vowel character remains at its original index.

The string may be large, so avoid repeated string rebuilding that takes quadratic time.

Function

reverseVowels(s: String) → String

Examples

Example 1

s = "hello"return = "holle"

The vowels are e and o. Reversing them produces holle.

Example 2

s = "leetcode"return = "leotcede"

The vowel sequence e, e, o, e becomes e, o, e, e; consonants keep their positions.

Example 3

s = "aA"return = "Aa"

Uppercase and lowercase vowels both participate, and their original cases move with the characters.

Constraints

  • 0 <= s.length <= 10^6.
  • s contains printable ASCII characters.

More Google problems

See Google hiring insights
public String reverseVowels(String s) {
  // Write your code here.
}
s"hello"
expected"holle"
Checking account…