All problems
0049HardArrayStringSimulation

Notice Board Justification

Tracked in this browser only
Write code

Trains the technique from

LeetCode 68Text Justification

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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.

  1. 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.
  2. 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.
  3. 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.
  4. 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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def render_board(tokens: list[str], width: int) -> list[str]:
Java
public List<String> renderBoard(String[] tokens, int width)
September 7
Apply