-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path68. Text Justification.c
170 lines (138 loc) · 3.97 KB
/
68. Text Justification.c
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
68. 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."]
L: 16.
Return the formatted lines as:
[
"This is an",
"example of text",
"justification. "
]
Note: Each word is guaranteed not to exceed L in length.
click to show corner cases.
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.
*/
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
char *make_str1(char **words, int *lens, int x, int l, int width) {
char *s;
int i, j, k, num_of_space, rem_of_space;
s = malloc((width + 1) * sizeof(char));
//assert(s);
if (x > 1) {
num_of_space = (width - l) / (x - 1);
rem_of_space = (width - l) % (x - 1);
} else {
num_of_space = width - l;
rem_of_space = 0;
}
k = 0;
for (i = 0; i < x; i ++) {
strcpy(&s[k], words[i]);
k += lens[i];
for (j = 0; (i == 0 || i != x - 1) && j < num_of_space; j ++) {
s[k] = ' ';
k ++;
}
if (rem_of_space) {
s[k] = ' ';
k ++;
rem_of_space --;
}
}
s[k] = 0;
return s;
}
char *make_str2(char **words, int *lens, int x, int l, int width) {
char *s;
int i, k;
s = malloc(width * sizeof(char));
//assert(s);
k = 0;
for (i = 0; i < x; i ++) {
if (i) {
s[k] = ' ';
k ++;
}
strcpy(&s[k], words[i]);
k += lens[i];
}
while (k < width) {
s[k] = ' ';
k ++;
}
s[k] = 0;
return s;
}
char** fullJustify(char** words, int wordsSize, int maxWidth, int* returnSize) {
int *lens, sz, n, i, l, k;
char **p = NULL, *s;
int a, b;
sz = 10;
n = 0;
p = malloc(sz * sizeof(char *));
lens = malloc(wordsSize * sizeof(int));
//assert(p && lens);
k = 0; a = 0;
for (i = 0; i < wordsSize; i ++) {
l = strlen(words[i]);
//assert(l <= maxWidth);
lens[i] = l;
if (k + l + i - a > maxWidth) {
// form a line
s = make_str1(&words[a], &lens[a], i - a, k, maxWidth);
//printf("%s\n", s);
//free(s);
if (n == sz) {
sz *= 2;
p = realloc(p, sz * sizeof(char *));
//assert(p);
}
p[n ++] = s;
k = 0; a = i;
}
k += l;
}
s = make_str2(&words[a], &lens[a], i - a, k, maxWidth);
//printf("%s\n", s);
//free(s);
if (n == sz) {
sz *= 2;
p = realloc(p, sz * sizeof(char *));
//assert(p);
}
p[n ++] = s;
*returnSize = n;
free(lens);
return p;
}
/*
Difficulty:Hard
Total Accepted:57.2K
Total Submissions:300.4K
Companies LinkedIn Airbnb Facebook
Related Topics String
*/