BitFont Part 3 - Decode Run-Length-Encoded Rows
Learn this problemProblem statement
In the BitFont exercise, IBM Plex Serif 14 glyph rows are stored with run-length encoding. Decode every supplied row into its full bitmap representation as a string of 0 and 1 pixels.
Each character in an encoded row gives one run length:
0-9represent lengths0-9.a-zrepresent lengths10-35, soais10,bis11, and so on.
Decode each row from left to right. The first run contains white pixels, represented by 0. After every encoded character, alternate between white (0) and black (1) for the next run.
A zero-length run appends no pixels, but it still changes the pixel value used by the next run. Return the decoded rows in the same order as the input.
Practice sequence
- Part 1: Render a Character
- Part 2: Render a Word
- Part 3: Decode Run-Length-Encoded Rows (current)
Function
decodeRle(encodedRows: String[]) β String[]Examples
Example 1
encodedRows = ["23", "1a", "b0"]return = ["00111", "01111111111", "00000000000"]23 becomes two white pixels followed by three black pixels. In 1a, a represents a run of length 10. In b0, b represents eleven white pixels and the final zero-length black run adds nothing.
Example 2
encodedRows = ["05", "02", "00"]return = ["11111", "11", ""]Each row begins with a white run. A zero-length run adds no pixels, but the state still changes before the next run. Therefore 05 produces five black pixels, 02 produces two black pixels, and 00 produces an empty row.
Example 3
encodedRows = ["a2", "3c2"]return = ["000000000011", "00011111111111100"]The letter a represents length 10, and c represents length 12. Thus a2 is ten white pixels followed by two black pixels, while 3c2 is three white pixels, twelve black pixels, and two white pixels.
Constraints
0 <= encodedRows.length <= 100.0 <= encodedRows[i].length <= 100.- Every encoded row contains only
0-9anda-z. - Rows are decoded independently, so their decoded widths may differ.