1/22/2013

Text Justification

Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.
You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces' ' when necessary so that each line has exactly L characters.
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, the empty slots on the left will be assigned more spaces than the slots on the right.
For the last line of text, it should be left justified and no extra space is inserted between words.
For example, words["This", "is", "an", "example", "of", "text", "justification."] L16.
Return the formatted lines as:
[
   "This    is    an",
   "example  of text",
   "justification.  "
]
Note: Each word is guaranteed not to exceed L in length.
class Solution {
public:
    vector<string> fullJustify(vector<string> &words, int L) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int lb = 0, ub = 0;     //start and end index of the words in the line
        int chcount = 0;
        vector<string> ret;
        while (ub < words.size()) {
            chcount += words[ub].size() + 1;  // assume at least one space between words
            int next = ub + 1;
            if (next != words.size() && chcount + words[next].size() <= L) {
                ++ub;
            }
            else {
                string str;
                chcount -= ub - lb + 1;         // get real character count
                int gapcount = ub - lb;
                int sm = L - chcount;           // space remaining
                
                if (gapcount) {
                    int avgsm = sm / gapcount;
                    int remainder = sm % gapcount;
                    for (int i = lb; i <= ub; ++i) {
                        str.append(words[i]);
                        int space = 0;
                        if (ub != words.size() - 1) {
                            if (i != ub) {
                                space = avgsm;
                                if (remainder) {
                                    ++space;
                                    --remainder;
                                }
                            }
                        }
                        else {
                            if (i != ub) {
                                space = 1;
                            }
                            else {
                                space = sm - gapcount;
                            }
                        }
                        str.append(space, ' ');
                    }
                }
                else {
                    // one word
                    str.append(words[ub]);
                    str.append(sm, ' ');
                }
                ret.push_back(str);
                lb = ++ub;
                chcount = 0;
            }
        }
            
        return ret;
    }
};

No comments:

Post a Comment