Justify string algorithm
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A full string-justification algorithm formats text so each line reaches a target width by distributing spaces between words. The tricky part is not splitting words into lines. It is deciding how to distribute the extra spaces when a line has more than one word. Once you separate line packing from space distribution, the algorithm becomes much easier to reason about.
Pack Words Greedily into Each Line
The usual first step is greedy packing: keep adding words to the current line until the next word would exceed the line width.
The len(current) term accounts for the minimum one-space gaps between already packed words.
Justify One Line at a Time
Once you know which words belong on a line, compute how many spaces are needed to fill the width.
This is the core of full justification: distribute the remainder spaces from left to right after giving each gap its base share.
Put the Whole Algorithm Together
The full solution combines greedy line packing with line-specific formatting.
The last line is usually left-justified rather than fully justified. That is a formatting rule, not an algorithmic necessity, but it is part of the standard version of the problem.
Why the Leftmost Gaps Get the Extra Spaces
When the space count does not divide evenly, typical text-justification rules place the larger gaps earlier in the line. That keeps the result deterministic and avoids ugly ambiguity in the output.
This detail matters because many incorrect solutions distribute spaces inconsistently or only approximately, which makes them fail exact-output tests.
Think About Edge Cases Early
Single-word lines, the last line, empty input, and words longer than the target width all need explicit decisions. Most challenge versions assume no single word exceeds the maximum width, but real text formatting code may need hyphenation or overflow rules too.
That is why justification algorithms are often simpler in coding interviews than in full text-layout engines.
Another useful check is deterministic output. If the same input can produce different spacing patterns depending on loop order or leftover arithmetic, exact-output tests will fail even when the line widths look visually correct.
Common Pitfalls
- Mixing line packing and space distribution into one hard-to-debug loop.
- Forgetting that the last line is usually left-justified.
- Handling uneven space distribution inconsistently.
- Failing on single-word lines.
- Ignoring assumptions about words that are longer than the line width.
Summary
- String justification has two main steps: pack words into lines, then distribute spaces.
- Greedy packing is the standard strategy for choosing which words go on each line.
- Full justification distributes extra spaces as evenly as possible across gaps.
- The last line is usually left-justified.
- Correct handling of edge cases is what separates a clean solution from a fragile one.
Related reading

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.