#3775
Reverse Words With Same Vowel Count
MediumTwo PointersStringSimulation
Approaches
💡
Intuition
Time Space
Count vowels in the first word, then check each subsequent word. Reverse words with matching vowel counts.
⚙️
Algorithm
3 steps- 1Step 1: Split the string into words.
- 2Step 2: Count vowels in the first word.
- 3Step 3: For each following word, reverse it if it has the same vowel count as the first word.
solution.py7 lines
1def reverseWords(s):
2 words = s.split()
3 first_vowel_count = sum(1 for c in words[0] if c in 'aeiou')
4 for i in range(1, len(words)):
5 if sum(1 for c in words[i] if c in 'aeiou') == first_vowel_count:
6 words[i] = words[i][::-1]
7 return ' '.join(words)Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.