Skip to content

S2Q2 · Reverse Vowel Order in a String

⚡ Quick Reference

Type: Full I/O -multiline string processing

Core idea: collect all vowels from the entire input, reverse them, then fill them back into the vowel positions left to right.

n = int(input())
lines = [input() for _ in range(n)]
text = "\n".join(lines)

vowels = "aeiouAEIOU"
vowel_list = [c for c in text if c in vowels]
vowel_list.reverse()

result = []
vi = 0
for c in text:
    if c in vowels:
        result.append(vowel_list[vi])
        vi += 1
    else:
        result.append(c)

print("".join(result))

Key rules: - Treat the entire multiline input as one string (newlines are non-vowels, stay in place) - Collect all vowels → reverse → fill back into vowel positions - Case is preserved -the reversed vowel goes into the slot, keeping its own case


Problem Statement

Problem (I/O type)

Read n lines. Reverse the order of all vowels across the entire input while keeping every non-vowel character in its original position. Print the modified multiline string.

Example:

Input
1
HEllo
Output
HollE

Understanding the problem

Two passes through the string:

  1. Extract all vowels in order → ['E', 'o']
  2. Reverse the list → ['o', 'E']
  3. Rebuild by scanning left to right: whenever you hit a vowel position, take the next vowel from the reversed list
Original:  H  E  l  l  o
           ↓  ↓  ↓  ↓  ↓
Vowel?     N  Y  N  N  Y

Vowels extracted: ['E', 'o']
Reversed:         ['o', 'E']

Rebuild:
  H  →  H  (non-vowel, keep)
  E  →  o  (vowel slot, take next from reversed list)
  l  →  l  (non-vowel, keep)
  l  →  l  (non-vowel, keep)
  o  →  E  (vowel slot, take next from reversed list)

Result: H o l l E  →  "HollE" 

Newlines are non-vowels

Joining all lines with "\n" and treating the whole thing as one string means newlines are processed as non-vowels -they stay in place, preserving the line structure in the output.


Tracing a multiline example

Input (2 lines): "Hello" and "World"

Joined text: "Hello\nWorld"

Vowels:     e, o, o  →  ['e', 'o', 'o']
Reversed:              ['o', 'o', 'e']

Rebuild:
H → H
e → o   (1st reversed vowel)
l → l
l → l
o → o   (2nd reversed vowel)
\n → \n
W → W
o → e   (3rd reversed vowel)
r → r
l → l
d → d

Result: "Hello\nWerld" split back → "Hollo" / "Werld"

Solution approaches

n = int(input())
lines = [input() for _ in range(n)]
text = "\n".join(lines)

vowels = "aeiouAEIOU"

# Pass 1 -collect all vowels
vowel_list = []
for c in text:
    if c in vowels:
        vowel_list.append(c)

# Reverse
vowel_list.reverse()

# Pass 2 -rebuild
result = []
vi = 0
for c in text:
    if c in vowels:
        result.append(vowel_list[vi])
        vi += 1
    else:
        result.append(c)

print("".join(result))
n = int(input())
text = "\n".join(input() for _ in range(n))

vowels = "aeiouAEIOU"
vowel_stack = [c for c in text if c in vowels][::-1]

vi = 0
result = []
for c in text:
    if c in vowels:
        result.append(vowel_stack[vi])
        vi += 1
    else:
        result.append(c)

print("".join(result))

[::-1] reverses the vowel list in one step. The rest is the same two-pass rebuild.

n = int(input())
text = "\n".join(input() for _ in range(n))

vowels = "aeiouAEIOU"
reversed_vowels = iter([c for c in text if c in vowels][::-1])

result = "".join(
    next(reversed_vowels) if c in vowels else c
    for c in text
)
print(result)

iter() on the reversed vowel list gives an iterator. next(reversed_vowels) pops the next vowel each time a vowel position is hit. The generator expression builds the result in one pass after setup.

n = int(input())
text = "\n".join(input() for _ in range(n))

vowels = "aeiouAEIOU"
rv = iter([c for c in text if c in vowels][::-1])

result = "".join(map(
    lambda c: next(rv) if c in vowels else c,
    text
))
print(result)

map(lambda c: ..., text) applies the replacement rule to every character. When a vowel is encountered, next(rv) pops the next reversed vowel. Fully functional pipeline.


Key takeaways

01

Extract → reverse → fill back

The standard pattern for "reverse only certain elements in place". Extract targets into a list, reverse it, then use an iterator to fill them back into the same positions.

02

iter() + next() for stateful consumption

iter(list) creates a stateful iterator. Each call to next() advances it -perfect for consuming elements one at a time inside a loop or map().

03

Join lines first, split back via print

Treating multiline input as one string with "\n".join(lines) simplifies the logic -newlines are just non-vowel characters. print() handles the output format automatically.