Hard · Strings
Word wrap
Given an array of ASCII words and a width maxWidth, split the words, in order, into lines (words on a line joined by single spaces, each line at most maxWidth long) that minimize the sum of (maxWidth − line length)² over every line except the last, and return the lines; return [] for no words or null if any word is longer than maxWidth.
Examples
Example 1
["aaa","bb","cc","ddddd"] w=6Output: ["aaa","bb cc","ddddd"]
Example 2
["a","b","c","d"] w=5Output: ["a b c","d"]
Rebuild it in the studio
Open every published interview problem, solution, scene, and memory room free, with no account.