Given an array of strings
You should pack your
Return the earliest hour at which the shop must be closed to incur a minimum penalty.
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.
- 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 exceedmaxWidth . -
The input array
words contains at least one word.
Output:
[ "This is an", "example of text", "justification. " ]
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.
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
Contents
Solution 1 - Loop through words array, and when maxLength is reached including spaces, apply spaces and add to result
In this approach, we will loop through the input array of
Implementation steps:
-
Create a variable
result asList<String> to hold the final list of strings, and two more variablescurrentLine asList<String> to track current list of strings that we are reading, but not yet processed,currentLength to track the length of strings that we are currently reading. -
Then, loop through each
word fromwords array, and check if thecurrentLength (total length of words that we read so far) +currentLine size (number of words so far) +new word's length that we are currently looking at, if this is greater thanmaxLength , that means with the words collected so far are enough to fit into themaxLength with text justification.-
Now, find total number of spaces that it can accommodate between the words collected so far.
totalSpaces = maxLength - currentLength; -
Then, check how many of these total spaces can be distributed equally among the lsit of words seen so far, if there are
m spaces andn words, that means we have to distribute betweenn-1 words. So, we can get the distribution of spaces by dividingm withn-1 , but if there is only one word in the list, then it will lead to divide by zero error, so to avoid that we can dividem withMath.max(1 , n-1) .spacesDistro = totalSpaces / Math.max(1, currentLine.size() -1); -
After distributing the total spaces equally between the
currentLine words, there still may be some spaces left, for example if there are 3 words and 5 spaces, then there will be one space left after distributing two spaces between the words, and we need to distribute these in a greedy fashion starting from left. To get how many such spaces by using modular operation on the same formula as above.remainingSpaces = totalSpaces % Math.max(1, currentLine.size() -1); -
Now, append the
spacesDistro number of spaces between for each word incurrentLine words, if there are more than one word, but if there is only one word, then append at the end of that word, then also appendremainingSpaces starting from first word in thecurrentLine words. -
Once the
currentLine of words are justified, then add this toresult , and resetcurrentLine andcurrentLength for next iteration of words.
-
Now, find total number of spaces that it can accommodate between the words collected so far.
-
If the
maxLength condition is not met, then simply add the currentword tocurrentLine list of words, and incrementcurrentLength by the current word length. -
At the end, we will still be left with the last line, and last line is left justified, we will add one space between words and add all remaining spaces at the end,
and add this to the
result .
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(n) for the result list.
Above implementations source code can be found at