Skip to main content

Reverse Words With Same Vowel Count - Solution & Explanation

Practice this problem

Problem Statement

You are given a string s consisting of lowercase English words, each separated by a single space.

Determine how many vowels appear in the first word. Then, reverse each following word that has the same vowel count. Leave all remaining words unchanged.

Return the resulting string.

Vowels are 'a', 'e', 'i', 'o', and 'u'.

 

Example 1:

Input: s = "cat and mice"

Output: "cat dna mice"

Explanation:​​​​​​​

  • The first word "cat" has 1 vowel.
  • "and" has 1 vowel, so it is reversed to form "dna".
  • "mice" has 2 vowels, so it remains unchanged.
  • Thus, the resulting string is "cat dna mice".

Example 2:

Input: s = "book is nice"

Output: "book is ecin"

Explanation:

  • The first word "book" has 2 vowels.
  • "is" has 1 vowel, so it remains unchanged.
  • "nice" has 2 vowels, so it is reversed to form "ecin".
  • Thus, the resulting string is "book is ecin".

Example 3:

Input: s = "banana healthy"

Output: "banana healthy"

Explanation:

  • The first word "banana" has 3 vowels.
  • "healthy" has 2 vowels, so it remains unchanged.
  • Thus, the resulting string is "banana healthy".

 

Constraints:

  • 1 <= s.length <= 105
  • s consists of lowercase English letters and spaces.
  • Words in s are separated by a single space.
  • s does not contain leading or trailing spaces.

Approach Overview

Problem Overview: You receive a sentence containing multiple words separated by spaces. The task is to reverse the order of words that have the same number of vowels while keeping the rest of the sentence structure intact.

Approach 1: Direct Simulation (O(n) time, O(n) space)

Split the sentence into a list of words using the space delimiter. For each word, compute the vowel count by iterating through its characters and checking membership in a vowel set such as {a,e,i,o,u}. Maintain a mapping from vowel count to the list of indices where those words appear. Once all words are processed, iterate through each index list and reverse the corresponding words in place by swapping elements from both ends of the list.

This works because words with the same vowel count form independent groups. Reversing the indices inside each group changes their relative order without affecting other words. The algorithm scans the sentence once to compute counts and once more to perform swaps, giving O(n) total time where n is the number of characters (or words depending on implementation). Extra storage for the mapping and split words results in O(n) space.

Approach 2: Two Pointers on Vowel Groups (O(n) time, O(n) space)

After computing the vowel count for each word, store the indices of words for each vowel count in an array or hash map. For every group of indices, apply a classic Two Pointers technique: place one pointer at the start and another at the end of the index list, then swap the words at those positions while moving both pointers inward.

This avoids creating intermediate reversed lists and keeps operations strictly in-place on the word array. The main operations are simple index lookups and swaps, making the approach efficient and easy to implement using basic String manipulation and Simulation. The complexity remains O(n) time and O(n) auxiliary space due to the grouped indices.

Recommended for interviews: The grouped simulation with two pointers is the expected solution. It demonstrates that you can preprocess the input, categorize elements by a computed property (vowel count), and then apply in‑place reversal efficiently. A brute-force idea might attempt repeated scans to find matching vowel counts, but the grouped approach shows stronger algorithmic thinking and keeps the runtime linear.

Solution

We first split the string by spaces into a word list words. Then we calculate the number of vowels cnt in the first word. Next, we iterate through each subsequent word, calculate its number of vowels, and if it equals cnt, reverse the word. Finally, we rejoin the processed word list into a string and return it.

The time complexity is O(n), and the space complexity is O(n), where n is the length of the string s.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Re-scanningO(n^2)O(1)Conceptual baseline when first reasoning about matching vowel counts
Simulation with Vowel Count MappingO(n)O(n)General solution; easy to implement and clearly groups words by vowel count
Two Pointers on Index GroupsO(n)O(n)Preferred implementation when reversing each vowel-count group efficiently

Video Solution

LeetCode 3775. Reverse Words With Same Vowel Count | Weekly Contest 480yoBro310 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Reverse Words With Same Vowel Count easy or hard?
Reverse Words With Same Vowel Count is typically categorized as a Medium problem. The difficulty comes from recognizing that words must be grouped by vowel count before performing the reversal. Once the grouping step is clear, the remaining logic is straightforward simulation with two pointers.
Reverse Words With Same Vowel Count Python/Java solution
The implementation is nearly identical across languages. Split the string into words, compute vowel counts with a helper function, group indices in a hash map, and reverse each group using two pointers. Python uses dictionaries and lists, while Java uses HashMap<Integer, List<Integer>> with array or list swaps.
How to solve Reverse Words With Same Vowel Count in O(n)?
Split the sentence into words, compute the vowel count for each word, and store its index in a map keyed by that count. For each group of indices, use two pointers to swap the words at the start and end of the group until the pointers meet. Because every word is processed a constant number of times, the algorithm remains linear.
What is the best approach for Reverse Words With Same Vowel Count?
The best approach groups words by their vowel count and then reverses the words within each group using a two‑pointer technique. First split the sentence, compute vowel counts, and store indices in a hash map keyed by the count. Reverse each index group in place. This runs in O(n) time with O(n) extra space.
Is Reverse Words With Same Vowel Count asked at Google/Amazon/Meta?
Problems involving grouping words by properties and performing in-place reversals frequently appear in coding interviews at large tech companies. Variants that combine string processing, hash maps, and two-pointer techniques are common in Google, Amazon, and Meta interview question sets.
What data structure is used in Reverse Words With Same Vowel Count?
The typical implementation uses an array or list for the words and a hash map that groups word indices by vowel count. Two pointers are then applied within each group to reverse the order. This combination keeps the solution simple and efficient.
What is the time complexity of Reverse Words With Same Vowel Count?
The optimal solution runs in O(n) time where n is the total number of characters or words in the sentence. Each word is scanned once to count vowels and each group is reversed with linear swaps. Auxiliary storage for grouped indices leads to O(n) space complexity.

Ready to solve this problem?

Practice Reverse Words With Same Vowel Count with our built-in code editor and test cases.

Practice on FleetCode