↓Skip to main content
  1. LeetCode/

LeetCode 3775: Reverse Words With Same Vowel Count

·1 min· ·
LeetCode Medium String Simulation Two-Pointers
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-24

  • Submissions: Accepted (first try)

Approach
#

  1. Split the string on spaces.
  2. Count the vowels of the first word, target.
  3. Leave the first word alone; reverse each later word whose vowel count equals target.

Reversing a word can use slicing, word[::-1], or converging pointers.

My Solution
#

class Solution:
    def reverseWords(self, s: str) -> str:
        def vowels(word: str) -> int:
            return sum(ch in "aeiou" for ch in word)

        words = s.split(" ")
        target = vowels(words[0])
        return " ".join([words[0]] + [w[::-1] if vowels(w) == target else w for w in words[1:]])

My original counted vowels by calling word.count(v) once per vowel, scanning the word five times; this version scans once. It also detected the first word by checking whether res was empty; here the first word is handled separately.

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Reversing the first word too: only the words after it are processed.

Interview Explanation
#

I split on spaces, count the first word’s vowels, reverse each later word with the same count and keep the others, then join with spaces. Each character is looked at a constant number of times, so time is O(n).

Related

LeetCode 2105: Watering Plants II
·2 mins
LeetCode Medium Two-Pointers Simulation Array
LeetCode 2105 note: Alice from the left and Bob from the right, simulated with converging pointers; when they meet at the same plant, whoever has more water waters it
LeetCode 1616: Split Two Strings to Make Palindrome
·3 mins
LeetCode Medium Two-Pointers String Greedy
LeetCode 1616 note: greedily match a’s prefix against b’s suffix from the outside in; the leftover middle must be a palindrome entirely within a or b, and both directions need checking
LeetCode 1750: Minimum Length of String After Deleting Similar Ends
·3 mins
LeetCode Medium Two-Pointers String
LeetCode 1750 note: when both ends match, each side eats its whole run of that character; a string of one repeated character can be deleted entirely
LeetCode 165: Compare Version Numbers
·1 min
LeetCode Medium Two-Pointers String
LeetCode 165 note: split on dots, compare revision by revision as integers, and treat missing revisions of the shorter version as 0
LeetCode 3703: Remove K-Balanced Substrings
·2 mins
LeetCode Medium Stack String Simulation
LeetCode 3703 note: repeated replace is O(n^2); a stack of (character, run length) removes k ( plus k ) as soon as they line up
LeetCode 3612: Process String with Special Operations I
·2 mins
LeetCode Medium String Simulation
LeetCode 3612 note: simulate the four operations with a list; put the “result is non-empty” check inside the * branch, or * gets appended as a letter