Format text into fixed-width justified lines
Format Text into Fixed-Width Justified Lines
You are given an array of strings words (each string is a word with no spaces) and an integer maxWidth.
Return a list of lines (strings) that formats the words into lines of exactly maxWidth characters each, following specific justification rules.
Rules
- Greedy Packing: Build lines from left to right using as many words as possible per line without exceeding
maxWidth. - No Word Splitting: A word must appear entirely on one line.
- **Space Distribution (Non-Last Lines):
- For a line with
kwords, there arek-1mandatory gaps between adjacent words, each requiring at least 1 space. - Distribute any remaining spaces as evenly as possible across all
k+1gaps (leading, between words, and trailing). - Extra spaces are assigned one by one from the leftmost gap (leading gap first, then left-to-right).
- Lines may start and/or end with spaces.
- For a line with
- Last Line: Left-justify the last line with a single space between words and all remaining spaces at the end.
Input / Output
- Input:
words: string[],maxWidth: int - Output:
string[]where each string has length exactlymaxWidth.
Example
If words = ["a","b","c"] and maxWidth = 5:
All three words fit on one line as the last line: "a b c" has length 5, so output is ["a b c"].
Constraints
1 <= words.length <= 3001 <= len(words[i]) <= maxWidth <= 100words[i]contains no spaces.
AI interview prep
Core dilemma
Balancing the greedy word packing strategy with the complex, non-uniform space distribution rules to precisely meet the `maxWidth` constraint for each line, especially handling the edge cases of the last line and lines with single words.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Format text into fixed-width justified lines
Format Text into Fixed-Width Justified Lines
You are given an array of strings words (each string is a word with no spaces) and an integer maxWidth.
Return a list of lines (strings) that formats the words into lines of exactly maxWidth characters each, following specific justification rules.
Rules
- Greedy Packing: Build lines from left to right using as many words as possible per line without exceeding
maxWidth. - No Word Splitting: A word must appear entirely on one line.
- **Space Distribution (Non-Last Lines):
- For a line with
kwords, there arek-1mandatory gaps between adjacent words, each requiring at least 1 space. - Distribute any remaining spaces as evenly as possible across all
k+1gaps (leading, between words, and trailing). - Extra spaces are assigned one by one from the leftmost gap (leading gap first, then left-to-right).
- Lines may start and/or end with spaces.
- For a line with
- Last Line: Left-justify the last line with a single space between words and all remaining spaces at the end.
Input / Output
- Input:
words: string[],maxWidth: int - Output:
string[]where each string has length exactlymaxWidth.
Example
If words = ["a","b","c"] and maxWidth = 5:
All three words fit on one line as the last line: "a b c" has length 5, so output is ["a b c"].
Constraints
1 <= words.length <= 3001 <= len(words[i]) <= maxWidth <= 100words[i]contains no spaces.