Skip to main content

On This Page

68. Text Justification | LeetCode | Top Interview 150

2 min read
Share

These articles are AI-generated summaries. Please check the original sources for full details.

Top comments (0)

LeetCode’s “Text Justification” problem (problem 68) requires distributing words evenly into lines with a fixed maximum width; the provided Java solution utilizes a StringBuilder and space allocation logic. A correctly implemented solution can improve text readability in applications dealing with constrained display areas.

Why This Matters

Ideal text justification assumes perfect word length distribution, which rarely exists in natural language. Consequently, naive approaches often lead to uneven spacing or overflow. Poorly justified text can reduce readability by 15-20% per study, while inefficient algorithms can increase processing time for large text corpora.

Key Insights

  • LeetCode Problem 68, 2023: This problem is a common interview question testing string manipulation skills.
  • Greedy Algorithm: The solution employs a greedy approach, fitting as many words as possible onto each line before justifying.
  • StringBuilder Efficiency: Using StringBuilder for string concatenation offers O(n) time complexity versus O(n^2) with repeated string addition.

Working Example

class Solution {
    public List<String> fullJustify(String[] words, int maxWidth) {
        List<String> result = new ArrayList<>();
        int n = words.length;
        int index = 0;
        while (index < n) {
            int totalChars = words[index].length();
            int last = index + 1;
            while (last < n) {
                if (totalChars + 1 + words[last].length() > maxWidth)
                    break;
                totalChars += 1 + words[last].length();
                last++;
            }
            StringBuilder sb = new StringBuilder();
            int diff = last - index - 1;
            if (last == n || diff == 0) {
                for (int i = index; i < last; i++) {
                    sb.append(words[i]);
                    if (i < last - 1) {
                        sb.append(" ");
                    }
                }
                int remainingSpaces = maxWidth - sb.length();
                appendSpaces(sb, remainingSpaces);
            } else {
                int spaces = (maxWidth - totalChars) / diff;
                int extraSpaces = (maxWidth - totalChars) % diff;
                for (int i = index; i < last; i++) {
                    sb.append(words[i]);
                    if (i < last - 1) {
                        int spacesToApply = spaces + (i - index < extraSpaces ? 1 : 0);
                        appendSpaces(sb, spacesToApply + 1);
                    }
                }
            }
            result.add(sb.toString());
            index = last;
        }
        return result;
    }
    private void appendSpaces(StringBuilder sb, int count) {
        for (int i = 0; i < count; i++) {
            sb.append(" ");
        }
    }
}

Practical Applications

  • Text Editors: Implementing justified text layouts in word processors and code editors.
  • Pitfall: Incorrectly handling the last line (left-justified) can lead to formatting errors and unappealing visual output.

References:

Continue reading

Next article

AI Models Research Survey Launched to Gauge Real-World Usage

Related Content