Introduction
LeetCode 68: Text Justification is a classic string formatting problem. It asks you to justify text so that each line fits a given width, and spaces are distributed as evenly as possible between words. The last line should be left-justified.
This problem combines greedy line construction with space distribution logic, and it's commonly used in interviews to test both string manipulation and algorithmic thinking.
Problem Statement
Given an array of words and a maximum width, format the text such that each line has exactly maxWidth
characters and is fully (left and right) justified.
Rules:
- You must pack as many words as possible in each line.
- Use spaces to fill in the gaps evenly.
- If the number of spaces can’t be evenly distributed, the extra spaces go to the leftmost gaps.
- The last line should be left-justified, and no extra space is inserted between words.
Examples:
go
Input:
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
Output:
[
"This is an",
"example of text",
"justification. "
]
Approach: Greedy Line Construction
- Start with an empty line and keep adding words until adding the next word would exceed
maxWidth
.
- Once the line is full:
- If it's the last line or has only one word, left-justify.
- Otherwise, distribute spaces between words to make the line fully justified.
- Repeat for all words.
Go Implementation
go
import "strings"
func fullJustify(words []string, maxWidth int) []string {
var res []string
i := 0
for i < len(words) {
lineLen := len(words[i])
j := i + 1
for j < len(words) && lineLen+1+len(words[j]) <= maxWidth {
lineLen += 1 + len(words[j])
j++
}
gaps := j - i - 1
line := ""
// If last line or only one word in the line
if j == len(words) || gaps == 0 {
for k := i; k < j; k++ {
line += words[k]
if k < j-1 {
line += " "
}
}
line += strings.Repeat(" ", maxWidth-len(line))
} else {
totalSpaces := maxWidth
for k := i; k < j; k++ {
totalSpaces -= len(words[k])
}
spacePerGap := totalSpaces / gaps
extraSpaces := totalSpaces % gaps
for k := i; k < j-1; k++ {
line += words[k]
spaces := spacePerGap
if k-i < extraSpaces {
spaces++
}
line += strings.Repeat(" ", spaces)
}
line += words[j-1] // Last word, no space after
}
res = append(res, line)
i = j
}
return res
}
Explanation
i
is the index of the current word.
- We try to fit as many words as possible into each line, tracking word length plus minimum spaces.
- If it's the last line or only one word, we use simple left-justification and fill the rest with spaces.
- Otherwise, we:
- Calculate how many spaces are needed between words.
- Distribute spaces equally.
- Distribute leftover spaces (
extraSpaces
) to the leftmost gaps.
Time and Space Complexity
MetricComplexityTime ComplexityO(n)Space ComplexityO(n)
Where n
is the total number of characters across all words. Each word is processed once.
Edge Cases
- Single word lines
- Last line handling
- Lines with words that perfectly fit the width
- Multiple short words (e.g.,
["a", "b", "c", "d", "e"]
)
Conclusion
LeetCode 68 is a powerful string manipulation challenge that requires careful planning and clean logic. It’s a favorite in interviews because it combines greedy algorithms, formatting, and attention to edge cases.
Writing this in Go gives you strong practice with slice handling and string concatenation patterns.