KatInterview

Stitch Glyph Rows Into a Banner

StringsAuto-graded · 19 test cases
EasyAuto-gradedStringsstringssimulationhash-map

Last updated

Combine per-character row strings from a font table into banner lines by joining row i of every glyph in order.

You are given a small bitmap font and a piece of text. Your job is to build the banner that shows the text in that font.

The font is a dictionary. Each key is a single character. Each value is a list of strings, one per pixel row of that character, from top to bottom. Each row string contains only the characters '0' and '1'.

Build the banner as a list of strings, one per pixel row. Banner line r is the concatenation, in text order, of row r of the glyph for each character of the text.

Rules:

  • Do not convert the bits. The output keeps '0' and '1' exactly as they appear in the font.
  • Do not insert any spacing or separator between glyphs.
  • Glyphs may have different widths. Concatenate each row as it is, without padding.
  • The same character can appear several times in the text. Use the same glyph each time.
  • If text is empty, return an empty list.
  • Every character in text is present in font. All glyphs in a given call have the same number of rows.

Return the list of banner lines, top to bottom.

Constraints

  • 0 <= len(text) <= 10^4
  • Each glyph has between 1 and 64 rows, and every glyph in one call has the same row count.
  • Each row string has length between 1 and 64 and contains only '0' and '1'.
  • Every character of text is a key in font.

Examples

Example 1

Input: build_banner({'A': ['0110', '1001', '1111', '1001', '1001'], 'B': ['1110', '1001', '1110', '1001', '1110']}, 'AB')

Output: ['01101110', '10011001', '11111110', '10011001', '10011110']

Example 2

Input: build_banner({'H': ['1001', '1001', '1111', '1001', '1001'], 'I': ['111', '010', '010', '010', '111']}, 'HI')

Output: ['1001111', '1001010', '1111010', '1001010', '1001111']

Example 3

Input: build_banner({'X': ['1001', '1001', '0110', '1001', '1001'], 'O': ['0110', '1001', '1001', '1001', '0110']}, 'XOX')

Output: ['100101101001', '100110011001', '011010010110', '100110011001', '100101101001']

Hints

Hint 1

Think in terms of output lines. Line r of the banner depends only on row r of each glyph, so build the output one line at a time.

Hint 2

For a fixed row index, collect that row from the glyph of each character in the text, in order, and join the pieces into one string.

Hint 3

Handle empty text first, since there is no glyph to read the height from. Use str.join for each line rather than repeated += to keep the work linear in the output size.