 New update is available. Click here to update. Close
Topic list
Justify Text
HARD
35 mins 14 upvotes
Strings
Arrays
Topics (Covered in this problem)
Problem solved
Skill meter Strings
-
- Arrays
-
-
Other topics
Problem solved
Skill meter Matrices (2D Arrays)
-
- -
- Sorting
-
- Binary Search
-
- Stacks & Queues
-
- Trees
-
- Graph
-
- Dynamic Programming
-
- Greedy
-
- Tries
-
- SQL
-
- Binary Search Trees
-
- Heap
-
- Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

# Justify Text

Hard 0/120
Avg time to solve 35 mins
Success Rate 50 % Share 14 upvotes

## Problem Statement

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

##### For example:
``````Let the given sentence be: [ “what”, “must”, “be”, “shall”, “be.”]
And L=12.

The justified output will be :

[ “what must be”
“Shall be.”       ]

Note that the last line is only left justified.
``````
Detailed explanation ( Input/output format, Notes, Constraints, Images ) ##### Constraints:
``````1 <= T <= 100
1 <= words.length <= 300
1 <= words[i].length <= 20
words[i] consists of only English letters and symbols.
1 <=L <= 100
words[i].length <= L

Where ‘T’ is the number of test cases, words.length denotes the number of words in the array and words[i].length denotes the number of alphabets in each word
L denotes the number of character in each line of the result.

Time Limit: 1 sec
``````
##### Sample Input 1:
``````2
7
This is an example of text justification.
16
3
I like apple
6
``````
##### Sample Output 1:
``````This    is    an
example  of text
justification.
I like
apple
``````
##### Explanation of Sample Input 1:
``````For the first test case,
We have 7 words in the sentence and we can have 16 characters in each line. So we will have the output as given above.
In each line we need 16 characters, we see that the first 3 words have 4+2+2 =8 characters and add 2 gaps. I.e 10 characters. now if we take one more word i.e example, we exceed the total number of characters in the line so we can take only 3 characters. We have 8 characters and 8 spaces. Which need to be distributed between 2 gaps. So each gap will have 4 spaces.

For the second test case,
We have 3 words in the sentence and 6 characters in each line. So we will have output as given above
``````
##### Sample Input 2:
``````2
9
When there is a will there is a way
10
4
Coding ninjas is great
10
``````
##### Sample Output 2:
`````` When there
is  a will
there is a
way
Coding
ninjas  is
great
``````  Auto Console