FunctionRacerProblemsRace

Expand Character Ranges

MediumStringsPython, JavaScript, TypeScript, Java, C++

pattern is a compact description of a string, read from left to right. A lowercase letter or digit that is followed by - and a second character of the same kind is a range: it stands for every character from the first one to the second one, inclusive, in alphabet or digit order. Any other lowercase letter or digit stands for itself. Return the expanded string.

For example "a-dx2-5" expands to "abcdx2345". The input always follows these rules: the two ends of a range are both lowercase letters or both digits, the first end is never later than the second (c-c is just c), every - is the middle of a range, and the character right after a range is never -. An empty pattern gives "".

Example 1

pattern "a-dx2-5"
returns "abcdx2345"

a-d gives abcd, x stands for itself and 2-5 gives 2345.

Example 2

pattern "abc"
returns "abc"

There is no -, so every character stands for itself.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Strings problems