1+ """
2+ Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left
3+ and right) justified.
4+
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
6+ ' ' when necessary so that each line has exactly L characters.
7+
8+ Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide
9+ evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
10+
11+ For the last line of text, it should be left justified and no extra space is inserted between words.
12+
13+ For example,
14+ words: ["This", "is", "an", "example", "of", "text", "justification."]
15+ L: 16.
16+
17+ Return the formatted lines as:
18+ [
19+ "This is an",
20+ "example of text",
21+ "justification. "
22+ ]
23+ Note: Each word is guaranteed not to exceed L in length.
24+
25+ click to show corner cases.
26+
27+ Corner Cases:
28+ A line other than the last line might contain only one word. What should you do in this case?
29+ In this case, that line should be left-justified.
30+ """
31+ __author__ = 'Danyang'
32+ class Solution :
33+ def fullJustify (self , words , L ):
34+ """
35+
36+ :param words: a list of str
37+ :param L: int
38+ :return: a list of str
39+ """
40+ result = []
41+ self .break_line (words , L , result )
42+ return self .distribute_space (L , result )
43+
44+ def break_line (self , words , L , result ):
45+ if not words :
46+ return
47+
48+ cur_length = - 1
49+ lst = []
50+ i = 0
51+ while i < len (words ):
52+ word = words [i ]
53+ cur_length += 1 # space in left justified
54+ cur_length += len (word )
55+ if cur_length > L : break
56+ lst .append (word )
57+ i += 1
58+
59+ result .append (lst )
60+ self .break_line (words [i :], L , result )
61+
62+
63+ def distribute_space (self , L , result ):
64+ new_result = []
65+ for ind , line in enumerate (result ):
66+ word_cnt = len (line )
67+ str_builder = []
68+ space_cnt = L - sum (len (word ) for word in line )
69+ hole_cnt = word_cnt - 1
70+ if ind < len (result )- 1 :
71+ if hole_cnt > 0 :
72+ space = space_cnt / hole_cnt
73+ remain = space_cnt % hole_cnt
74+
75+ for word in line [:- 1 ]:
76+ str_builder .append (word )
77+ str_builder .append (" " * space )
78+ if remain > 0 :
79+ str_builder .append (" " )
80+ remain -= 1
81+
82+ str_builder .append (line [- 1 ])
83+ else :
84+ str_builder .append (line [- 1 ])
85+ str_builder .append (" " * space_cnt )
86+ else : # last line, special handling
87+ str_builder = [" " .join (line )]
88+ str_builder .append (" " * (space_cnt - hole_cnt ))
89+
90+ new_result .append ("" .join (str_builder ))
91+
92+ return new_result
93+
94+
95+
96+ if __name__ == "__main__" :
97+ print Solution ().fullJustify (["This" , "is" , "an" , "example" , "of" , "text" , "justification." ], 16 )
98+ print Solution ().fullJustify (["What" ,"must" ,"be" ,"shall" ,"be." ], 12 )
0 commit comments