Dsa
Stack
Hard
String Parentheses Compression
Implement a program that compresses a given string consisting of nested parentheses and letters. For each pair of matching parentheses, replace it with the letter that was inside. If the parentheses are empty, replace them with a single underscore (_). Use a stack to achieve this.
Input format:
A single line containing the input string with letters and parentheses.
Output format:
Print the compressed string according to the given rules.
Example:
Input:
(a(b)c)
Output:
abc
Key concepts
stackcompressionparentheses
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now