classSolution{public:vector<string>fullJustify(vector<string>&words,size_tmaxWidth){vector<string>ans;vector<string>row;size_trowLetters=0;for(conststring&word:words){// If we put the word in this row, it'll exceed the maxWidth,// So we cannot put the word to this row and have to pad spaces to// Each word in this rowif(rowLetters+row.size()+word.length()>maxWidth){constintspaces=maxWidth-rowLetters;if(row.size()==1){// Pad all spaces after row[0]for(inti=0;i<spaces;++i)row[0]+=" ";}else{// Evenly pad spaces to each word (expect the last one) in this rowfor(inti=0;i<spaces;++i)row[i%(row.size()-1)]+=" ";}ans.push_back(join(row,""));row.clear();rowLetters=0;}row.push_back(word);rowLetters+=word.length();}ans.push_back(ljust(join(row," "),maxWidth));returnans;}private:stringjoin(constvector<string>&v,conststring&c){strings;for(autop=begin(v);p!=end(v);++p){s+=*p;if(p!=end(v)-1)s+=c;}returns;}stringljust(strings,intwidth){for(inti=0;i<s.length()-width;++i)s+=" ";returns;}};