Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

68. Text Justification

Hard46.0% Acceptance
ArrayStringSimulation
Asked by:
G
Google
ProblemSolutions (5)VideosCompanies (26)Notes

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 empty slots on the left will be assigned more spaces than the slots on the right.

For the last line of text, it should be left-justified, and no extra space is inserted between words.

Note:

  • A word is defined as a character sequence consisting of non-space characters only.
  • Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
  • The input array words contains at least one word.

Example 1:

Input: words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16
Output:
[
   "This    is    an",
   "example  of text",
   "justification.  "
]

Example 2:

Input: words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16
Output:
[
  "What   must   be",
  "acknowledgment  ",
  "shall be        "
]
Explanation: Note that the last line is "shall be    " instead of "shall     be", because the last line must be left-justified instead of fully-justified.
Note that the second line is also left-justified because it contains only one word.

Example 3:

Input: words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"], maxWidth = 20
Output:
[
  "Science  is  what we",
  "understand      well",
  "enough to explain to",
  "a  computer.  Art is",
  "everything  else  we",
  "do                  "
]

Constraints:

  • 1 <= words.length <= 300
  • 1 <= words[i].length <= 20
  • words[i] consists of only English letters and symbols.
  • 1 <= maxWidth <= 100
  • words[i].length <= maxWidth
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A
Amazon
C
Capital One
C
Coursera
e
eBay
+21

Approach

The Text Justification problem is typically solved using a greedy simulation approach. The idea is to process the list of words and build lines one by one while ensuring each line does not exceed the given maximum width. For each line, keep adding words until adding another word would exceed maxWidth.

Once the words for a line are selected, distribute the required spaces between them so the total length becomes exactly maxWidth. Spaces should be distributed as evenly as possible from left to right. If the spaces cannot be divided evenly, the left gaps receive extra spaces. The final line is treated differently—it is left-justified, meaning words are separated by a single space and the remaining spaces are added at the end.

This approach processes each word once and simulates spacing distribution efficiently, resulting in a linear pass over the input.

Time Complexity: O(n) where n is the total number of words (or characters processed). Space Complexity: O(1) auxiliary space excluding the output.

Complexity

ApproachTime ComplexitySpace Complexity
Greedy Line Building with Space DistributionO(n)O(1) auxiliary (excluding output)

Video Solution Available

Tushar Roy - Coding Made Simple

View all video solutions

Solutions (5)

Greedy Line Construction

This approach involves iterating through the given words and greedily forming a line by adding as many words as possible without exceeding the maxWidth. When a line is completed, the algorithm distributes spaces evenly between words. If the number of spaces doesn’t divide evenly, more spaces are added to the left slots. The last line is treated differently by left-justifying the text.

Time Complexity: O(n), where n is the total number of characters in all words.
Space Complexity: O(n), where n is the number of lines formed times maxWidth for the justified text.

PythonJavaJavaScript
1def fullJustify(words, maxWidth):
2    def justify_line(line, width, maxWidth):
3        if len(line) == 1:
4            return line[0] + ' ' * (maxWidth - width)
5        total_spaces = maxWidth - width + len(line) - 1
6        space, extra = divmod(total_spaces, len(line) - 1)
7        for i in range(extra):
8            line[i] += ' '
9        return (' ' * space).join(line)
10
11    res, line, width = [], [], 0
12    for word in words:
13        if width + len(word) + len(line) > maxWidth:
14            res.append(justify_line(line, width, maxWidth))
15            line, width = [], 0
16        line.append(word)
17        width += len(word)
18
19    last_line = ' '.join(line)
20    res.append(last_line + ' ' * (maxWidth - len(last_line)))
21    return res

Explanation

The code defines a helper function, justify_line, to construct each justified line. While adding words to the current line, if adding another word exceeds maxWidth, the line is justified and appended to the result. The last line is treated separately for left-justification.

Dynamic Programming Approach

This method leverages dynamic programming to minimize a hypothetical cost function that combines badness or uneven space distribution along with total number of lines used, aiming to determine the optimal setup of words per line.

Time Complexity: O(n^2), due to potential double iterations required for exploring word positions.
Space Complexity: O(n), since it uses extra space for storing dp and path arrays.

C++C#
1#include <iostream>
2#include <vector>
3#include <string>
4#include <climits>
5using namespace std;

vector<string> fullJustify(vector<string>& words, int maxWidth) {
    int n = words.size();
    vector<int> dp(n + 1, INT_MAX);
    vector<int> path(n);
    dp[n] = 0;
    for (int i = n - 1; i >= 0; --i) {
        int len = -1;
        for (int j = i; j < n; ++j) {
            len += words[j].length() + 1;
            if (len > maxWidth) break;
            int last_cost = j == n - 1 ? 0 : maxWidth - len;
            if (dp[i] > last_cost + dp[j + 1]) {
                dp[i] = last_cost + dp[j + 1];
                path[i] = j + 1;
            }
        }
    }
    
    vector<string> res;
    for (int i = 0; i < n; i = path[i]) {
        int j = path[i];
        string line = words[i];
        for (int k = i + 1; k < j; ++k) {
            line += " " + words[k];
        }
        line += string(maxWidth - line.length(), ' ');
        res.push_back(line);
    }
    return res;
}

Video Solutions

Watch expert explanations and walkthroughs

Text Justification Dynamic Programming

Tushar Roy - Coding Made Simple
15:31145,258 views

Asked By Companies

26 companies
G
Google
A
Amazon
C
Capital One
C
Coursera
e
eBay
M
Microsoft
N
Nvidia
O
Oracle
P
Pinterest
S
Snapchat
F
Flipkart
L
LinkedIn
Q
Quikr
W
Walmart
A
Adobe
W
Wadhwani AI
N
Nagarro
L
Leverage Edu
K
Karat
U
Uber
F
Facebook
S
Square
A
Apple
R
Reddit
T
Twilio
I
Indeed

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Group AnagramsMedium
Word SearchMedium
Word BreakMedium
Word Break IIHard
More similar problems

Related Topics

ArrayStringSimulation

Problem Stats

Acceptance Rate46.0%
DifficultyHard
Companies26

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Text Justification asked in FAANG interviews?

Yes, Text Justification is a common interview-style problem that tests string manipulation, greedy thinking, and attention to detail. Variants of this problem have appeared in coding interviews at large tech companies.

What data structure is best for Text Justification?

Arrays or lists are typically used to store the words for the current line. String builders or similar structures help efficiently construct the final justified line with properly distributed spaces.

What is the optimal approach for Text Justification?

The optimal approach uses a greedy strategy. You group as many words as possible into each line without exceeding the maximum width, then distribute spaces evenly between words. Special handling is applied to the last line, which is left-justified.

Why is Text Justification considered a Hard problem?

The challenge lies in correctly distributing spaces between words and handling edge cases such as single-word lines and the final left-justified line. Careful simulation and string manipulation are required to meet all formatting constraints.

Previous Problem

Plus One

Next Problem

Set Matrix Zeroes

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

Explanation

This C++ implementation uses a dynamic programming table, dp, to compute the minimum penalty of breaking words into lines. path is used to store optimal partitioning. The final step reconstructs justified text using information stored in path.