Class Solution

java.lang.Object
g0001_0100.s0068_text_justification.Solution

public class Solution extends Object
68 - Text Justification.<p>Hard</p> <p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p> <p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces <code>' '</code> when necessary so that each line has exactly <code>maxWidth</code> characters.</p> <p>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.</p> <p>For the last line of text, it should be left-justified and no extra space is inserted between words.</p> <p><strong>Note:</strong></p> <ul> <li>A word is defined as a character sequence consisting of non-space characters only.</li> <li>Each word&rsquo;s length is guaranteed to be greater than 0 and not exceed maxWidth.</li> <li>The input array <code>words</code> contains at least one word.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> words = [&ldquo;This&rdquo;, &ldquo;is&rdquo;, &ldquo;an&rdquo;, &ldquo;example&rdquo;, &ldquo;of&rdquo;, &ldquo;text&rdquo;, &ldquo;justification.&rdquo;], maxWidth = 16</p> <p><strong>Output:</strong> [ &ldquo;This is an&rdquo;, &ldquo;example of text&rdquo;, &quot;justification. &quot; ]</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> words = [&ldquo;What&rdquo;,&ldquo;must&rdquo;,&ldquo;be&rdquo;,&ldquo;acknowledgment&rdquo;,&ldquo;shall&rdquo;,&ldquo;be&rdquo;], maxWidth = 16</p> <p><strong>Output:</strong> [ &ldquo;What must be&rdquo;, &quot;acknowledgment &quot;, &quot;shall be &quot; ]</p> <p><strong>Explanation:</strong> Note that the last line is &quot;shall be &quot; instead of &ldquo;shall be&rdquo;, 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.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> words = [&ldquo;Science&rdquo;,&ldquo;is&rdquo;,&ldquo;what&rdquo;,&ldquo;we&rdquo;,&ldquo;understand&rdquo;,&ldquo;well&rdquo;,&ldquo;enough&rdquo;,&ldquo;to&rdquo;,&ldquo;explain&rdquo;,&ldquo;to&rdquo;,&ldquo;a&rdquo;,&ldquo;computer.&rdquo;,&ldquo;Art&rdquo;,&ldquo;is&rdquo;,&ldquo;everything&rdquo;,&ldquo;else&rdquo;,&ldquo;we&rdquo;,&ldquo;do&rdquo;], maxWidth = 20</p> <p><strong>Output:</strong> [ &ldquo;Science is what we&rdquo;, &ldquo;understand well&rdquo;, &ldquo;enough to explain to&rdquo;, &ldquo;a computer. Art is&rdquo;, &ldquo;everything else we&rdquo;, &quot;do &quot; ]</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= words.length <= 300</code></li> <li><code>1 <= words[i].length <= 20</code></li> <li><code>words[i]</code> consists of only English letters and symbols.</li> <li><code>1 <= maxWidth <= 100</code></li> <li><code>words[i].length <= maxWidth</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details