forked from aylei/leetcode-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn0068_text_justification.rs
177 lines (168 loc) · 5.46 KB
/
n0068_text_justification.rs
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
171
172
173
174
175
176
177
/**
* [68] Text Justification
*
* Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth 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 maxWidth 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.
*
* Note:
*
*
* A word is defined as a character sequence consisting of non-space characters only.
* Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
* The input array words contains at least one word.
*
*
* Example 1:
*
*
* Input:
* words = ["This", "is", "an", "example", "of", "text", "justification."]
* maxWidth = 16
* Output:
* [
* "This is an",
* "example of text",
* "justification. "
* ]
*
*
* Example 2:
*
*
* Input:
* words = ["What","must","be","acknowledgment","shall","be"]
* maxWidth = 16
* Output:
* [
* "What must be",
* "acknowledgment ",
* "shall be "
* ]
* Explanation: Note that the last line is "shall be " instead of "shall be",
* because the last line must be left-justified instead of fully-justified.
* Note that the second line is also left-justified becase it contains only one word.
*
*
* Example 3:
*
*
* Input:
* words = ["Science","is","what","we","understand","well","enough","to","explain",
* "to","a","computer.","Art","is","everything","else","we","do"]
* maxWidth = 20
* Output:
* [
* "Science is what we",
* "understand well",
* "enough to explain to",
* "a computer. Art is",
* "everything else we",
* "do "
* ]
*
*
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn full_justify(words: Vec<String>, max_width: i32) -> Vec<String> {
let mut res = Vec::new();
let max_width = max_width as usize;
let mut i = 0;
let mut row_len = 0;
let mut buf = Vec::new();
while i < words.len() {
if words[i].len() > max_width { unreachable!() }
let old_len = row_len;
row_len += words[i].len() + if row_len > 0 { 1 } else { 0 };
if row_len > max_width {
res.push(Solution::compact(buf, max_width, old_len));
buf = Vec::new();
row_len = 0;
} else {
buf.push(words[i].clone());
i += 1;
}
}
res.push(Solution::compact_last(buf, max_width));
res
}
fn compact(words: Vec<String>, max_width: usize, row_len: usize) -> String {
let spaces = max_width - (row_len - words.len() + 1);
let avg_spaces = spaces / usize::max(1, words.len() - 1);
let mut extra_spaces = spaces - avg_spaces * usize::max(1, words.len() - 1);;
let mut res = String::new();
for (i, word) in words.iter().enumerate() {
res.push_str(word);
if words.len() < 2 || (i < words.len() - 1) {
res.push_str(&" ".repeat(avg_spaces));
if extra_spaces > 0 {
res.push(' ');
extra_spaces -= 1;
}
}
}
res
}
fn compact_last(words: Vec<String>, max_width: usize) -> String {
let mut res = String::new();
for (i, word) in words.iter().enumerate() {
res.push_str(word);
if i < words.len() - 1 {
res.push(' ');
}
}
res.push_str(&" ".repeat(max_width - res.len()));
res
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_68() {
assert_eq!(
Solution::full_justify(
vec_string!["This", "is", "an", "example", "of", "text", "justification."],
16
),
vec_string![
"This is an",
"example of text",
"justification. "
]
);
assert_eq!(
Solution::full_justify(
vec_string!["What","must","be","acknowledgment","shall","be"],
16
),
vec_string![
"What must be",
"acknowledgment ",
"shall be "
]
);
assert_eq!(
Solution::full_justify(
vec_string!["Science","is","what","we","understand","well","enough","to","explain",
"to","a","computer.","Art","is","everything","else","we","do"],
20
),
vec_string![
"Science is what we",
"understand well",
"enough to explain to",
"a computer. Art is",
"everything else we",
"do ",
]
);
}
}