Celebrating the success of our 25 days, let's have a format of each DSA concepts for 10 days. Let's start with Strings!
Problem Statement
You are given a compressed string where segments can include:
- Lowercase and uppercase English letters
- Digits
- Special characters/punctuation (
!@#$%^&*()_+-=[]{}|;:',.<>?
)
Each segment in the compressed string consists of a character followed by its frequency (e.g., a3
means "aaa"
, @2
means "@@"
). Consecutive segments are concatenated without spaces or delimiters. You must decompress the string by expanding each segment.
Special Rules
- Uppercase and lowercase letters are treated distinctly (e.g.,
A2
is"AA"
anda2
is"aa"
). - Digits that appear as characters in segments are expanded (e.g.,
4
in43
means"444"
, where the first4
is a character and3
is its frequency). - Special characters are expanded just like letters or digits (e.g.,
@2
is"@@"
).
Your task is to reconstruct the original string by expanding all segments.
Input Format
- A single line containing the compressed string .
Output Format
- A single line containing the decompressed string.
Constraints
- (length of the compressed string).
- Each frequency is a positive integer between 1 and 99.
- contains only valid segments.
Example Input 1
Example Output 1
Example Input 2
Example Output 2
Example Input 3
Example Output 3
Explanation
In the first example:
a3
expands to"aaa"
B2
expands to"BB"
@4
expands to"@@@@"
#1
expands to"#"
In the second example:
x5
expands to"xxxxx"
Y1
expands to"Y"
!3
expands to"!!!"
z2
expands to"zz"
In the third example:
A2
expands to"AA"
b1
expands to"b"
C4
expands to"CCCC"
$5
expands to"$$$$$"
Python Code
Notes
- The problem introduces complexity with diverse character sets and larger constraints.
- Using regular expressions, the program efficiently extracts characters and their corresponding frequencies for reconstruction.
- Edge cases include segments like
A1
(output:"A"
),#2
(output:"##"
), and long strings to ensure the solution scales well.
Comments
Post a Comment