LeetCode 68: Text Justification Solution

Master LeetCode problem 68 (Text Justification), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

68. Text Justification

Problem Explanation

Explanation:

To solve this problem, we can follow these steps:

  1. Iterate over the words list and greedily form lines by adding words until the line length exceeds the maxWidth.
  2. Calculate the total number of spaces required to evenly distribute among words in each line.
  3. Handle the special case when there is only one word in a line or it is the last line.
  4. Construct the final result by formatting the lines with appropriate spaces.

Time Complexity: O(N), where N is the total number of characters in all words.

Space Complexity: O(N), where N is the total number of characters in all words.

:

Solution Code

class Solution {
    public List<String> fullJustify(String[] words, int maxWidth) {
        List<String> result = new ArrayList<>();
        int start = 0;
        
        while (start < words.length) {
            int end = start + 1;
            int lineLength = words[start].length();
            
            while (end < words.length && lineLength + words[end].length() + (end - start) <= maxWidth) {
                lineLength += words[end].length();
                end++;
            }
            
            int spaces = maxWidth - lineLength;
            int gaps = end - start - 1;
            
            StringBuilder sb = new StringBuilder();
            sb.append(words[start]);
            
            if (gaps == 0 || end == words.length) {
                for (int i = start + 1; i < end; i++) {
                    sb.append(" ").append(words[i]);
                }
                for (int i = sb.length(); i < maxWidth; i++) {
                    sb.append(" ");
                }
            } else {
                int spacesPerGap = spaces / gaps;
                int extraSpaces = spaces % gaps;
                
                for (int i = start + 1; i < end; i++) {
                    for (int j = 0; j < spacesPerGap; j++) {
                        sb.append(" ");
                    }
                    if (extraSpaces > 0) {
                        sb.append(" ");
                        extraSpaces--;
                    }
                    sb.append(words[i]);
                }
            }
            
            result.add(sb.toString());
            start = end;
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 68 (Text Justification)?

This page provides optimized solutions for LeetCode problem 68 (Text Justification) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 68 (Text Justification)?

The time complexity for LeetCode 68 (Text Justification) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 68 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 68 in Java, C++, or Python.

Back to LeetCode Solutions