1class Solution {2string preprocessing(string &f, int space, int limit) {3int noofspace = limit - f.size() + space;4int k = 0;5string r = "";6while (space) {7int n = noofspace / space;8if ((noofspace % space) != 0) n += 1;9noofspace -= n;10while (f[k] != ' ') {11r += f[k];12k++;13}14k++;15while (n--) {16r += ' ';17}18space--;19}20while (k < f.size()) {21r += f[k];22k++;23}24while (noofspace--) r += ' ';25return r;26}2728public:29vector<string> fullJustify(vector<string> &words, int maxWidth) {30vector<string> ans;31string f = "";32int space = 0;33for (string &str : words) {34if ((f.size() + str.size() + 1) <= maxWidth && f.size() > 0) {35f += ' ' + str;36space++;37} else {38if (f.size() > 0) {39f = preprocessing(f, space, maxWidth);40ans.push_back(f);41f = "";42}43f += str;44space = 0;45}46}47int sz = f.size();48while (sz < maxWidth) {49f += ' ';50sz++;51}52ans.push_back(f);53return ans;54}55};