Wrap Words from Existing Text Lines
Problem statement
Given existing text lines and maxLen, preprocess the input into one ordered sequence of words and wrap those words into new lines. Old source-line boundaries do not force output boundaries.
For this exercise, assume each output line uses the largest consecutive group that fits with exactly one ASCII space between words. Add no trailing spaces or padding, preserve spelling and repeated occurrences, and return an empty array when there are no words.
For this exercise, assume every word contains only English letters and individually fits the maximum.
For this exercise, assume ASCII spaces separate input words; ignore leading, trailing and repeated spaces in source strings.
Function
wrapLines(lines: String[], maxLen: int) → String[]Examples
Example 1
lines = ["Hello Sir","Please Upvote","If You Like My Post"]maxLen = 10return = ["Hello Sir","Please","Upvote If","You Like","My Post"]This complete fixture is authored; the source gives no literal words or output lines.
Example 2
lines = ["a bb"," c dd "]maxLen = 5return = ["a bb","c dd"]This authored fixture preserves word order and uses one space between words.
Example 3
lines = [""," ","Abcd e"]maxLen = 4return = ["Abcd","e"]This authored fixture has a fitting singleton and no line padding.
Constraints
- For this exercise, assume
0 <= lines.length <= 100and at most2000total input characters. - For this exercise, assume
1 <= maxLen <= 100. Each word has length from 1 through maxLen and contains only uppercase or lowercase English letters. - Input text may also contain ASCII spaces.