Expand Character Ranges
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
a-d gives abcd, x stands for itself and 2-5 gives 2345.
Example 2
There is no -, so every character stands for itself.
Constraints
0 <= len(pattern) <= 30patternhas lowercase letters, digits and-only- the expanded string has at most 60 characters
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Strings problems
- Falling DominoesMedium
- Fit the Capital PatternMedium
- Kth Letter of the Unpacked TextMedium
- Solve for xMedium
- String to IntegerMedium
- Substrings With Every VowelMedium
- Undo the Column ReadMedium
- Valid Decimal LiteralMedium