Text Justification in C++


Suppose we have an array of words and a width maxWidth, we have to format the text such that each line has exactly maxWidth number of characters and is fully justified. We should pack our words in a greedy approach; so that is, pack as many words as we can in each line. We will pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters.

Here extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, empty slots on the left will be assigned more spaces than the slots on the right. For the final line of text, it should be left justified and no extra space is inserted between words.

So if the input is like

To solve this, we will follow these steps −

  • create one array called result

  • for i in range 0 to size of a, update i by j

    • width := 0

    • for j in range i to size of a and width + size of a[j] + j – i <= b,

      • width := width + size of a[j]

    • space := 1, extra := 0

    • if j – 1 != 1 and j != size of a, then

      • space := (b - width) / j – i – 1

      • extra := (b - width) mod (j – i – 1)

    • line := a[i]

    • for k in range i + 1 to j

      • concatenate space number of blank-spaces with line

      • if extra > 0, then concatenate space with line

      • decrease extra by 1

      • line := concatenate a[k] with line

    • x := size of line

    • line := concatenate (b - x) number of spaces with line

    • insert line into result

  • return res

Example

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
void print_vector(vector<vector<auto>> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << "[";
      for(int j = 0; j <v[i].size(); j++){
         cout << v[i][j] << ", ";
      }
      cout << "],";
   }
   cout << "]"<<endl;
}
class Solution {
   public:
   vector<string> fullJustify(vector<string> &a, int b) {
      vector <string> result;
      int i, j;
      for(i = 0; i < a.size(); i = j){
         int width = 0;
         for(j = i; j < a.size() && width + a[j].size() + j - i <= b; j++){
            width += a[j].size();
         }
         int space = 1;
         int extra = 0;
         if(j - i != 1 && j != a.size()){
            space = (b - width) / (j - i - 1);
            extra = (b - width) % (j - i - 1);
         }
         string line(a[i]);
         for(int k = i + 1; k < j; k++){
            line += string(space, ' ');
            if(extra-- > 0){
               line += " ";
            }
            line += a[k];
         }
         int x = line.size();
         line += string(b - x, ' ');
         result.push_back(line);
      }
      return result;
   }
};
main(){
   vector<string> v = {"I", "love", "coding.", "here", "we", "will", "write", "some", "program"};
   Solution ob;
   print_vector(ob.fullJustify(v, 16));
}

Input

["I", "love", "coding.", "here", "we", "will", "write", "some", "program"]
16

Output

[I love coding.,
here we will,
write some,
program ,
]

Updated on: 26-May-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements