Text Justification
Text Justification
Given an array of strings words and an integer maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified. The words should be packed greedily onto each line, meaning as many words as possible are placed on a single line. Extra spaces should be added between words to meet the maxWidth. These extra spaces should be distributed as evenly as possible. If the spaces cannot be divided evenly, the left gaps should receive more spaces than the right gaps.
The last line of text must be left-justified, and no extra spaces should be inserted between words on this line.
Assumptions:
- A word is a sequence of non-space characters.
- Each word's length is greater than 0 and does not exceed
maxWidth. - The input array
wordscontains at least one word.
Constraints:
1 <= words.length <= 3001 <= words[i].length <= 20words[i]consists of only English letters and symbols.1 <= maxWidth <= 100words[i].length <= maxWidth
Similar LeetCode problem: Text Justification
AI interview prep
Core dilemma
Balancing the greedy packing of words with the precise distribution of spaces to achieve full justification, while handling edge cases like the last line and lines with a single word.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Text Justification
Text Justification
Given an array of strings words and an integer maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified. The words should be packed greedily onto each line, meaning as many words as possible are placed on a single line. Extra spaces should be added between words to meet the maxWidth. These extra spaces should be distributed as evenly as possible. If the spaces cannot be divided evenly, the left gaps should receive more spaces than the right gaps.
The last line of text must be left-justified, and no extra spaces should be inserted between words on this line.
Assumptions:
- A word is a sequence of non-space characters.
- Each word's length is greater than 0 and does not exceed
maxWidth. - The input array
wordscontains at least one word.
Constraints:
1 <= words.length <= 3001 <= words[i].length <= 20words[i]consists of only English letters and symbols.1 <= maxWidth <= 100words[i].length <= maxWidth
Similar LeetCode problem: Text Justification