Text Justification
LeetCode 68 · Hard · solution, intuition & step-by-step walkthrough
Greedy packing + even/remainder space distribution.
This is how I solved Text Justification (LeetCode 68), a hard problem from the Other pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a line-by-line debugger.
Intuition
Greedily pack as many words as fit on each line. Then distribute the leftover spaces between words, giving the leftmost gaps one extra space when they don't divide evenly. The last line, and any one-word line, is left-justified instead.
Approach
- 1
create_line: take words while length + word ≤ maxWidth (counting one space each).
- 2
extra = maxWidth − (letters + minimum spaces).
- 3
Last line or single word: join with single spaces and pad the right.
- 4
Otherwise give each gap extra // gaps spaces and the first extra % gaps gaps one more.
How I solved it
Greedy line builderPythonO(n) time
class Solution:
def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
#get no of words in each line - create_line
#Prepare line with spaces
#append the line
def create_line(i):
line = []
cur_length = 0
while i<len(words) and cur_length+len(words[i])<=maxWidth:
cur_length+=len(words[i])+1
line.append(words[i])
i+=1
return line
def prepare_line(i,line):
#prepare what are the word counts, each word will have one space except last word
#extra space maxwidhth- (each word+1)=>baseline-1
#if we are at end line, or there is only one word, just append spaces
#if not then add odd spaces on left, even spaces based on word count
#return the final line
word_counts=len(line)-1
baseline=0
for word in line:
baseline+=len(word)+1
baseline-=1 #don't need for last word
extra_space = maxWidth-baseline
if i==len(words) or len(line)==1:
return " ".join(line)+" "*extra_space
even_spaces = extra_space//word_counts
odd_spaces = extra_space%word_counts
for i in range(odd_spaces):
line[i]+=" "
for i in range(word_counts):
line[i]+=" "*even_spaces
return " ".join(line)
res=[]
i=0
while i<len(words):
line = create_line(i)
i+=len(line)
res.append(prepare_line(i,line)) #i is sent post increment to check whether we have reached at the end of the line
return resText Justification — FAQ
What is the intuition behind Text Justification?
Greedily pack as many words as fit on each line. Then distribute the leftover spaces between words, giving the leftmost gaps one extra space when they don't divide evenly. The last line, and any one-word line, is left-justified instead.
How do you solve Text Justification (LeetCode 68)?
1. create_line: take words while length + word ≤ maxWidth (counting one space each). 2. extra = maxWidth − (letters + minimum spaces). 3. Last line or single word: join with single spaces and pad the right. 4. Otherwise give each gap extra // gaps spaces and the first extra % gaps gaps one more.
What is the time and space complexity of Text Justification?
Time: O(total characters). Space: O(maxWidth). (Accepted approach: Greedy line builder.)
What are common mistakes in Text Justification?
Left gaps get the remainder, not the right ones.