Stitch Glyph Rows Into a Banner
StringsAuto-graded · 19 test casesLast 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
textis empty, return an empty list. - Every character in
textis present infont. 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
textis a key infont.
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.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
A terminal prints one line at a time, so the banner has to be built row by row, not glyph by glyph. Line r of the result is row r of the first glyph, followed by row r of the second glyph, and so on through the text.
- If
textis empty, return[]. - Read the number of rows
heightfrom the glyph of the first character. All glyphs have the same height. - Look up each character's glyph once, in text order.
- For each row index
rfrom0toheight - 1, join rowrof every glyph into one string. - Return the list of those strings.
Why it is correct
Each glyph stores its rows top to bottom, so index r is the same pixel row in every glyph. Joining those entries in text order places the glyphs side by side with no gaps. Because we only concatenate and never pad or convert, glyphs of different widths and the original '0'/'1' characters come through unchanged. Repeated characters just reuse the same row lists.
Complexity
Let n be the text length, H the number of rows, and W the typical glyph width.
- Time:
O(n * H * W), which is linear in the size of the output and so optimal. - Space:
O(n * H * W)for the result, plusO(n)for the list of glyph references.
Reference solution
from typing import Dict, List
def build_banner(font: Dict[str, List[str]], text: str) -> List[str]:
if not text:
return []
height = len(font[text[0]])
glyphs = [font[ch] for ch in text]
return ["".join(g[row] for g in glyphs) for row in range(height)]