Friday, March 6, 2015

Text Justification

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 exactlyL 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.
Corner Cases:
  • A line other than the last line might contain only one word. What should you do in this case?
    In this case, that line should be left-justified.
---------------------------------- thinking -----------------------------------------
https://oj.leetcode.com/discuss/25181/my-3ms-c-solution
there are a lot of edge  cases we need to pay attention to in this question:
Input: [""], 2
Output: [""]
Expected: ["  "]

Input: ["a"], 1
Output: ["a","a"]
Expected: ["a"]


Input: ["Here","is","an","example","of","text","justification."], 16
Output: ["Here    is    an","example       of","text            ","justification.  "]
Expected: ["Here    is    an","example  of text","justification.  "]


Input: ["Listen","to","many,","speak","to","a","few."], 6
Output: ["Listen","to    ","many, ","speak ","to a","few.  "]
Expected: ["Listen","to    ","many, ","speak ","to   a","few.  "]
----------------------------------- codes ---------------------------------------------
     class Solution {
   public:
       vector<string> fullJustify(vector<string> &words, int L) {
           if (L == 0) return words;
           int ind = 0;
           int pre = 0;
           int count = 0;
           vector<string> result;
           while (ind < words.size()) {
               count += words[ind].size() + 1;
               //bug here
               if (count > L) {
                   //bug here!
                   if (ind == pre) {
                       int sub = L - words[ind].size();
                       string ele = words[ind];
                       if (sub > 0) {
                           ele += string(sub, ' ');
                       }
                       result.push_back(ele);
                       count = 0;
                       pre = ind + 1;
                       //bug here
                   } else if (count == L + 1) {
                       string ele;
                       for (int i = pre; i < ind; i++) {
                           ele += words[i];
                           ele += " ";
                       }
                       ele += words[ind];
                       result.push_back(ele);
                       count = 0;
                       pre = ind + 1;
                   } else {
                       int num = ind - pre - 1;
                       // bug here
                       int spc = L - (count - words[ind].size() - 1) + num + 1;
                       string ele;
                       if (num == 0) {
                           ele = words[pre] + string(spc, ' ');
                       } else {
                           int even_grid = spc / num;
                           int remain_grid = spc % num;
                           int cur_grid;
                           for (int i = pre; i < ind - 1; i++) {
                               ele += words[i];
                               cur_grid = even_grid + ((remain_grid > 0)?1:0);
                               remain_grid--;
                               ele += string(cur_grid, ' ');
                           }
                           ele += words[ind - 1];
                       }
                       result.push_back(ele);
                       pre = ind;
                       count = 0;
                       ind--;
                   }
               }
               ind++;
           }
           if (pre < ind) {
           string ele;
           for (int i = pre; i < ind - 1; i++) {
               ele += words[i];
               ele += " ";
           }
           ele += words[words.size() - 1];
           int sub = L - ele.size();
           ele += string(sub, ' ');
           result.push_back(ele);
           }
           return result;
       }
   };

No comments:

Post a Comment