[leetcode_68]Text Justification

So excited! Finally AC’d all LeetCode problems at once! Although the journey had its twists and turns, I’m still thrilled!
This is a simulation problem – formatting strings according to specified rules. The great thing about LeetCode is that you can see the test data, understand the problem statement thoroughly, and debug step by step. Getting it AC on the first try is actually quite difficult.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class Solution {
public:
    vector<string> fullJustify(vector<string> &words, int L) {
        vector<vector<string>> result;
        result.clear();
        vector<string> ans;
        ans.clear();
        if (L == 0) {
            ans.push_back("");
            return ans;
        }
        for (int i = 0; i < words.size(); i++) {
            vector<string> item;
            item.clear();
            int sum = 0;
            while (true) {
                if (item.size() > 0) sum += 1;
                if (sum >= L) {
                    i--;
                    break;
                }
                if (i >= words.size()) break;
                item.push_back(words[i]);
                sum += words[i].length();
                if (sum > L) {
                    item.pop_back();
                    i--;
                    break;
                }
                i++;
            }
            result.push_back(item);
        }
        for (int i = 0; i < result.size(); i++) {
            string item = "";
            if (i != result.size() - 1) item = genString(result[i], L);
            else
                item = genStringLast(result[i], L);
            ans.push_back(item);
        }
        return ans;
    }
private:
    string genStringLast(vector<string> &item, int L) {
        int sum = 0;
        string str = "";
        for (int i = 0; i < item.size(); i++) {
            str += item[i];
            sum += item[i].size();
            if (i != item.size() - 1) {
                str += " ";
                sum += 1;
            }
        }
        for (int i = 0; i < L - sum; i++)
            str += " ";
        return str;
    }
    string genString(vector<string> &item, int L) {
        int sum = 0;
        for (int i = 0; i < item.size(); i++)
            sum += item[i].size();
        string str = "";
        if (item.size() == 1) {
            int countS = L - sum;
            str += item[0];
            for (int j = 0; j < countS; j++)
                str += " ";
            return str;
        }
        int spaces;
        if (item.size() > 1) spaces = (L - sum) / (item.size() - 1);
        else
            spaces = 0;
        int ex = L - sum - spaces * (item.size() - 1);
        for (int i = 0; i < item.size(); i++) {
            str += item[i];
            int countS;
            if (i < ex)
                countS = 1 + spaces;
            else
                countS = spaces;
            if (i != item.size() - 1)
                for (int j = 0; j < countS; j++)
                    str += " ";
        }
        return str;
    }
};