Dsa
Recursion
Easy
The Binary String Compressor
A binary string S of length 2^N can be compressed using a recursive rule:
1. If all characters in S are the same (all '0' or all '1'), the result is just that character ('0' or '1').
2. Otherwise, the result is '(' + compress(LeftHalf) + compress(RightHalf) + ')'.
Write a program to generate this compressed form.
Input Format:
A string of '0's and '1's (length is always a power of 2).
Output Format:
The compressed string.
Example:
Input: 11110000
Output: (10)
(Explanation: 1111 becomes 1, 0000 becomes 0, combined with parens yields (10).)
Key concepts
recursiondivide and conquerstring manipulation
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