I am doing a coding exercise on leetcode, but wondering, is this a good coding? Please help check https://leetcode.com/problems/text-justification/discuss/2017233
Here is the code copy:
class Solution{
private:
int wordsCnt;
int charCnt;
int emptyCnt;
vector<string> res;
void pairing(vector<string>& words, int maxWidth, int startIndex);
void centerJustify(vector<string>& words, int maxWidth, int startIndex);
void leftJustify(vector<string>& words, int maxWidth, int startIndex);
public:
vector<string> fullJustify(vector<string>& words, int maxWidth);
};
// Given the beginning index,
// 1. Get how many words is needed to build a line -> wordsCnt
// 2. Get how many empty space should be added -> emptyCnt
// 3. Get how many characters are in the line -> charCnt
void Solution::pairing(vector<string>& words, int maxWidth, int startIndex)
{
int spaceRemaining; // How many space is still available
// Reset
wordsCnt = 0;
charCnt = 0;
emptyCnt = 0;
spaceRemaining = maxWidth;
// When space available to add a further word &&
// Not reach the end of the words
while(((int)(words[startIndex + wordsCnt].size())) <= spaceRemaining)
{
charCnt += words[startIndex + wordsCnt].size();
wordsCnt++;
// Space remaining for further words
// Add an empty space after each word
spaceRemaining = maxWidth - charCnt - wordsCnt;
// Reach the end of the words
if ((startIndex + wordsCnt) >= (int)(words.size())) break;
}
emptyCnt = maxWidth - charCnt;
return;
}
void Solution::centerJustify(vector<string>& words, int maxWidth, int startIndex)
{
string str;
int repeat;
for(int i = 0; i < wordsCnt; i++)
{
// First get the average number of empty space
// Then add an additional empty space
// Example:
// 4 words -> wordsCnt = 4
// 5 empty spaces -> emptyCnt = 5
//
// -> average: emptyCnt / (wordsCnt-1) = 5 /(4 - 1) = 1
// -> additional for i=0: (5 % (4 - 1) > 0) = (2 > 0) = TRUE
// -> additional for i=1: (5 % (4 - 1) > 1) = (2 > 1) = TRUE
// -> additional for i=2: (5 % (4 - 1) > 1) = (2 > 2) = FALSE
repeat = emptyCnt / (wordsCnt-1) + (emptyCnt % (wordsCnt-1) > i);
// No empty space after last word
if(i == (wordsCnt-1))
{
repeat = 0;
}
str.append(words[startIndex+i]);
str.append(repeat, ' ');
}
res.push_back(str);
return;
}
void Solution::leftJustify(vector<string>& words, int maxWidth, int startIndex)
{
string str;
int repeat = 1;
for(int i = 0; i < wordsCnt; i++)
{
// Fill the empty space after the last word
if(i == (wordsCnt-1))
{
repeat = maxWidth - charCnt - wordsCnt + 1;
}
str.append(words[startIndex+i]);
str.append(repeat, ' ');
}
res.push_back(str);
return;
}
vector<string> Solution::fullJustify(vector<string>& words, int maxWidth)
{
// Reset
int startIndex = 0;
res.clear();
while(1)
{
// Step 1: Find the words to build a line
pairing(words, maxWidth, startIndex);
// Step 2: Build the line
if((startIndex+wordsCnt) < (int)(words.size()))
{
if(wordsCnt == 1)
{
// If the line has only one word, it is left justified
leftJustify(words, maxWidth, startIndex);
}
else
{
centerJustify(words, maxWidth, startIndex);
}
startIndex += wordsCnt;
}
else
{
// Last line
leftJustify(words, maxWidth, startIndex);
break;
}
}
return res;
}