public ArrayList<String> fullJustify(String[] words, int L) {
    ArrayList<String> res = new ArrayList<String>();
    if (words.length == 0)
      return res;

    ArrayList<String> tmp = new ArrayList<String>();
    StringBuilder sb = new StringBuilder();
    int cur = 0;
    int len = 0;

    while (cur < words.length) {
      sb = new StringBuilder();
      sb.append(words[cur]);

      len = words[cur].length();
      cur++;
      tmp.clear();

      while (cur < words.length && len + 1 + words[cur].length() <= L) {
        tmp.add(" " + words[cur]);
        len += words[cur].length() + 1;
        cur++;
      }

      if (cur < words.length && tmp.size() > 0) {
        int spaces = L - len;
        int avg = spaces / tmp.size();
        int rem = spaces % tmp.size();
        for (int i = 0; i < tmp.size(); i++) {
          appendSpace(sb, i < rem ? avg + 1 : avg);
          sb.append(tmp.get(i));
        }
      } else {
        for (int i = 0; i < tmp.size(); i++)
          sb.append(tmp.get(i));
        appendSpace(sb, L - len);
      }
      res.add(sb.toString());
    }
    return res;
  }