Reverse Words With Same Vowel Count - Solution & Explanation
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 <= 105sconsists of lowercase English letters and spaces.- Words in
sare separated by a single space. sdoes 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
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Re-scanning | O(n^2) | O(1) | Conceptual baseline when first reasoning about matching vowel counts |
| Simulation with Vowel Count Mapping | O(n) | O(n) | General solution; easy to implement and clearly groups words by vowel count |
| Two Pointers on Index Groups | O(n) | O(n) | Preferred implementation when reversing each vowel-count group efficiently |
Video Solution
LeetCode 3775. Reverse Words With Same Vowel Count | Weekly Contest 480 • yoBro • 310 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 Python/Java solution
How to solve Reverse Words With Same Vowel Count in O(n)?
What is the best approach for Reverse Words With Same Vowel Count?
Is Reverse Words With Same Vowel Count asked at Google/Amazon/Meta?
What data structure is used in Reverse Words With Same Vowel Count?
What is the time complexity of Reverse Words With Same Vowel Count?
Ready to solve this problem?
Practice Reverse Words With Same Vowel Count with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor