68. Text Justification Hard
1/**
2 * [68] Text Justification
3 *
4 * Given an array of strings words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified.
5 * 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.
6 * Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
7 * For the last line of text, it should be left-justified, and no extra space is inserted between words.
8 * Note:
9 *
10 * A word is defined as a character sequence consisting of non-space characters only.
11 * Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
12 * The input array words contains at least one word.
13 *
14 *
15 * Example 1:
16 *
17 * Input: words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16
18 * Output:
19 * [
20 * "This is an",
21 * "example of text",
22 * "justification. "
23 * ]
24 * Example 2:
25 *
26 * Input: words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16
27 * Output:
28 * [
29 * "What must be",
30 * "acknowledgment ",
31 * "shall be "
32 * ]
33 * 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.
34 * Note that the second line is also left-justified because it contains only one word.
35 * Example 3:
36 *
37 * Input: words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"], maxWidth = 20
38 * Output:
39 * [
40 * "Science is what we",
41 * "understand well",
42 * "enough to explain to",
43 * "a computer. Art is",
44 * "everything else we",
45 * "do "
46 * ]
47 *
48 * Constraints:
49 *
50 * 1 <= words.length <= 300
51 * 1 <= words[i].length <= 20
52 * words[i] consists of only English letters and symbols.
53 * 1 <= maxWidth <= 100
54 * words[i].length <= maxWidth
55 *
56 */
57pub struct Solution {}
58
59// problem: https://leetcode.com/problems/text-justification/
60// discuss: https://leetcode.com/problems/text-justification/discuss/?currentPage=1&orderBy=most_votes&query=
61
62// submission codes start here
63
64impl Solution {
65 pub fn full_justify(words: Vec<String>, max_width: i32) -> Vec<String> {
66 vec![]
67 }
68}
69
70// submission codes end
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75
76 #[test]
77 fn test_68() {
78 }
79}
80
Back
© 2025 bowen.ge All Rights Reserved.