Text Justification Problem (OR Word Wrap Problem)

Objec­tive:  Given a list of words and length L. Format the words so that each line will have only L characters and fully justified (left and right justified).


  • You need to fit as many as words in every line.
  • You are not allowed to split a word and put them in next line.
  • Pad extra spaces ‘ ‘ if needed so that each line has L characters.
  • Extra spaces between words should be distributed as evenly as possible. If even distribution is not possible then extra spaces will be assigned on the left most space (between two words).

Assumption – Each word has length less than Length L.


String [] words = {"This", "is", "a", "text", "justification","problem","in","tutorial","horizon"};
int length = 20;

This   is   a   text
problem  in tutorial

Approach: Recursion

Maintain an index which tracks the numbers of words already used (already justified)

  1. Say Length = L number of characters in each line.
  2. Start taking words into a new line.
  3. Keep track of number of characters adding to the new line.
  4. Add a special character (‘@’) after each word (later will replace by space ‘ ‘)
  5. Stop adding word to new line if Either adding new word will exceed the characters count in line from given length L Or characters count = Length L.
  6. Calculate the number of spaces needs to add to make the characters count to length L.
  7. Evenly distribute the spaces between the selected words. If any spaces are left (in case even distribution is not possible), add it to between first 2 words.
  8. Replace ‘@’ with spaces calculated in the step above.
  9. Make a recursive call for rest of the words (from step 2 to Step 8)
  10. See the code for better understanding.


public class TextJustification {
public String find(String [] words, int maxWidth, int index){
String result = "";
int currentLen = 0;
int remainLength = maxWidth;
while(result.length()<maxWidth && index<words.length){
if(remainLength>=words[index].length()+1){ //remainingLength+1 for space
result += "@" + words[index];
currentLen = words[index].length()+1;
result += words[index];
currentLen = words[index].length();
remainLength -= currentLen;
}else if(remainLength>0){
for (int i = 0; i < remainLength; i++) {
result = result + " ";
}else {
//go in only if there at least 2 words
String[] arr = result.split("@");
int mod = (remainLength % (arr.length1));
int splitedSpace = remainLength / (arr.length1);
String spaces = " ";
for (int i = 0; i < splitedSpace; i++) {
spaces = spaces + " ";
String leftmost = spaces;
for (int i = 0; i < mod; i++) {
leftmost = leftmost + " ";
result = result.replaceFirst("@", leftmost);
result = result.replaceAll("@", spaces);
result = result.replaceAll("@", " ");
return result + "\n" + find(words,maxWidth,index);
return "";
public static void main(String[] args) {
TextJustification t = new TextJustification();
String [] words = {"This", "is", "a", "text", "justification","problem","in","tutorial","horizon"};
int maxWidth = 25;


This      is    a    text
justification  problem in
tutorial horizon


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.