easy +10 pts

Expand compressed string

Decode a run-length encoded string like 'a3b2' into 'aaabb'.

Write a function `expand(s: str) -> str` that takes a run-length encoded string and returns the fully expanded string. The input consists of lowercase English letters (a–z) each optionally followed by a positive integer (1–99, no leading zeros). The encoded syntax is: a letter may be followed by a digit(s) indicating how many times it repeats. If no number follows a letter, it repeats once. For example, `'a3b2'` becomes `'aaabb'`, and `'ab2'` becomes `'abb'`. The input will never be empty, and it will always be a valid encoded string.

Constraints

1 ≤ length of s ≤ 1000. The decoded string length will be at most 100,000. You may not use any external libraries.

Example

>>> expand('a3b2')
'aaabb'
>>> expand('ab2')
'abb'
>>> expand('x10')
'xxxxxxxxxx'
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Iterate over the string and collect consecutive digits after a letter to form the full number.
When you see a letter, remember it. When you see a digit, accumulate it into a multiplier.
Use a while loop to safely read multi-digit numbers.
Remember: if a letter has no digits after it, it appears once.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.