Text Justification
Real-world version: Notice Board JustificationThe statement here is our own: the same inputs, outputs and technique.
A platform notice board prints one announcement at a time on a character cell display that is width cells across. The announcement arrives already chopped into pieces as tokens, in the order they must appear. No token contains a blank, and no token is longer than width.
Lay the tokens out row by row and return the rows as a list of strings, each string exactly width characters long, built by these rules.
- Filling. Walk the tokens in order and keep adding the next one to the row you are building for as long as it still fits, counting at least one blank cell between neighbouring tokens on that row. When it no longer fits, close the row and start the next one with that token.
- Stretching a closed row that holds two or more tokens. The row must reach exactly
width, so widen the blank runs between its tokens. Every gap gets the same number of blanks where that is possible. If the blanks do not divide evenly over the gaps, the surplus goes to the gaps nearest the left edge, one extra blank each, until the surplus is used up. So the leftmost gap is never narrower than a gap to its right. - A closed row holding one token. There is no gap to widen, so the token starts at the left edge and every remaining cell to its right is blank.
- The final row. It is never stretched. Its tokens are separated by exactly one blank, and the cells after the last token are blank out to
width. That holds even if the final row happens to carry several tokens.
Examples
Example 1
- Input
- tokens = ["depart", "gate", "nine", "boarding", "now"], width = 12
- Output
- ["depart gate", "nine ", "boarding now"]
Row one holds two tokens and one gap, so both surplus blanks land in it. Row two carries a single token padded on the right. The final row fills the display exactly, so no padding is needed.
Example 2
- Input
- tokens = ["red", "amber", "green", "flash"], width = 16
- Output
- ["red amber green", "flash "]
Row one has three blanks to spread over two gaps, so the left gap takes two and the right gap takes one. The final row is a lone token padded to the right edge.
Example 3
- Input
- tokens = ["cancelled", "delay", "sixty"], width = 9
- Output
- ["cancelled", "delay ", "sixty "]
The first token is as wide as the display, so nothing can share its row and it needs no padding at all.
Constraints
- 1 <= tokens.length <= 300
- 1 <= tokens[i].length <= 20
- tokens[i] is made of English letters and punctuation, and contains no blank
- 1 <= width <= 100
- tokens[i].length <= width
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def render_board(tokens: list[str], width: int) -> list[str]:public List<String> renderBoard(String[] tokens, int width)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 9 test cases: 9 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Final Value of Variable After Performing OperationsReal-world version: Hand Tally Reading After The LogEasy
- Weighted Word MappingReal-world version: Heaviest Label by Letter WeightEasy
- Longest Common PrefixReal-world version: Shared Crate CodeEasy
- Concatenation of ArrayReal-world version: Two Cycle PlayoutEasy