Solved LeetCode program · 68 of 1,000
#0068 · Text Justification
Original English source
The source is "This,is,an,example,of,text,justification.". The pieces is split source by ",". Keep a shared words as an empty list of text.
For each piece in pieces.
Place piece into words.
Keep a shared count of 7. Keep a shared width of 16. Keep a shared finish of 0. Keep a shared usedwidth of 0. Keep a shared padding as empty text. Keep a shared piece as empty text. Keep a shared padpiece as empty text. Set padding to 32 spaces.
To search from position (a whole number) with occupied (a whole number). If position is count, set finish to position, return. The word is item position of words. The wordlast is the last position of word. The nextusedwidth is occupied plus 2 plus wordlast. The previousused is occupied. Set usedwidth to nextusedwidth. If nextusedwidth is at most width, search from position plus 1 with nextusedwidth, return. Set usedwidth to previousused. Set finish to position.
To build line from start (a whole number). Set finish to 0. The firstword is item start of words. The firstlast is the last position of firstword. Set usedwidth to firstlast plus 1. Search from start plus 1 with usedwidth. The linewords is finish minus start. The gaps is linewords minus 1. The base is 0. The extra is 0. If finish is less than count and gaps is more than 0, set base to (width minus usedwidth plus gaps) divided by gaps, set extra to (width minus usedwidth plus gaps) modulo gaps. The line is empty text.
For every index from start to finish minus 1.
The word is item index of words. Place word into line. The gap is index minus start. The amount is base. If gap is less than extra, add 1 to amount. Set piece to the slice of padding from 0 to amount. If index is less than finish minus 1 and finish is count, Place " " into line. If index is less than finish minus 1 and finish is less than count, Place piece into line. If index is finish minus 1 and finish is count, set padpiece to the slice of padding from 0 to width minus usedwidth plus gaps, Place padpiece into line, report line. If index is finish minus 1 and finish is less than count, report line, build line from finish.
Build line from 0.
Expected stdout
This is an example of text justification.