vector<string> Solution::fullJustify(vector<string> &A, int B){ int l = 0, n = A.size(); vector<string> res; while(l < n) { int now = A[l].length(); int space = 0; int r = l + 1; while(r < n and now + space + 1 + A[r].length() <= B) { now += A[r++].length(); space += 1; } space = B - now; int eqspace = r-l-1 ? space / (r-l-1) : 0; int gap = B - now - eqspace * (r-l-1); string s = ""; if(r != n) { while(l < r) { s += A[l++] + string(eqspace, ' '); if(gap) { s.push_back(' '); gap--; } } } elsewhile(l < r) s += A[l++] + " "; while(s.length() < B) s.push_back(' '); while(s.length() > B) s.pop_back(); res.push_back(s); } return res; }