Advertisement

Text Justification - LeetCode 68 Solution

Text Justification - Complete Solution Guide

Text Justification is LeetCode problem 68, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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. 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 does not divide evenly between words, the

Detailed Explanation

The Text Justification problem requires formatting a given array of words into lines of text, each with a specified maximum width. The words must be packed greedily, meaning as many words as possible are placed on each line. The lines, except for the last one, should be fully justified (left and right). This involves distributing extra spaces evenly between words. If spaces can't be distributed evenly, the leftmost gaps should have more spaces. The last line must be left-justified with no extra spaces inserted between words.

Solution Approach

The provided solution uses a greedy approach to pack words into lines. It iterates through the words array, building each line one at a time. For each line, it determines how many words can fit within the maxWidth. Then, based on whether it's the last line or a line with a single word, it applies different justification rules. The solution calculates the required spaces, distributes them evenly where applicable, and constructs the final justified string for each line.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty list `result` to store the justified lines.
  2. Step 2: Iterate through the `words` array using a `while` loop.
  3. Step 3: For each line, determine the ending index `j` by greedily adding words until the `maxWidth` is exceeded.
  4. Step 4: Extract the words for the current line `line_words` from `i` to `j`.
  5. Step 5: Check if it is the last line or if `line_words` contains only one word.
  6. Step 6: If it is the last line or only one word, left-justify the line and add it to `result`.
  7. Step 7: If it is not the last line and contains more than one word, calculate the required number of spaces.
  8. Step 8: Distribute spaces evenly and add extra spaces from left to right.
  9. Step 9: Build the justified line using a string builder and append it to the `result`.
  10. Step 10: Update the starting index `i` to `j` and repeat until all words are processed.

Key Insights

  • Insight 1: The core task is to determine how many words fit on each line before exceeding the maxWidth. This involves iterating through the words and accumulating their lengths along with the spaces between them.
  • Insight 2: Distributing spaces evenly between words in a line requires calculating the base number of spaces and any remaining extra spaces. The extra spaces need to be distributed from left to right.
  • Insight 3: Handling the last line differently (left-justified) is a crucial edge case. Also, lines with only one word must be left-justified.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, String, Simulation.

Companies

Asked at: Adobe, Airbnb, Amazon, Apple, Atlassian, Bloomberg, Capital One, Coinbase, Coursera, Databricks, DevRev, Google, Karat, LinkedIn, Meta, Microsoft, MongoDB, Moveworks, Netflix, Notion, Oracle, PayPal, Robinhood, Roblox, SIG, Samsara, Sentry, TikTok, Twilio, Uber, Visa, WeRide, Zoho.