TopCoder problem "TextEditor" used in SRM 171 (Division II Level Three)

Problem Statement

    You have a page of text that is nicely formatted in the usual fashion (like this text), but you wanted it formatted in two columns, like a magazine. Given a String[] text, and an int width, return a String[] which is the same text but formatted such that every element contains as many words as possible without being more than width characters long. Begin by putting as many words as possible in the first line of the first column without using more than width characters, then continue this for each subsequent line. The elements of the return value should be organized such that every pair of elements corresponds to one line of text, spread over two columns. Thus element 2*i and 2*i+1 (with i starting at 0) of the output will represent the i-th line of the first and second columns, respectively. If the last line of the second column has no text, do not include it as part of the return value (see example 0). Do not hyphenate words to fit them on two lines. Also, there should only be a single space between adjacent words, and there should be no leading or trailing spaces in the lines. A 'word' is a sequence of non-space characters that does not have non-space characters immediately before or after it. Punctuation marks should be treated the same as other characters. For example, "Hello,Fred", "Hello,I'mFred", and "...!?!?" are all considered to be single words, and should not be broken apart in any way.

For example, if text={"This text is now ","in two columns."} and width=7, the output would be {"This", "two", "text is", "columns.", "now in"}. Making the first element of the return value the first line of the first column, the next element the first line of the second column, and then continuing in the same way for subsequent rows, the text looks like this:
"This"    "two"
"text is" "columns."
"now in"


Parameters:String[], int
Method signature:String[] twoColumn(String[] text, int width)
(be sure your method is public)


-text will contain between 1 and 50 elements, inclusive.
-width will contain between 1 and 50, inclusive.
-Each element of text will be between 1 and 50 characters, inclusive.
-Each element of text will contain only upper and lower-case letters ('a'-'z','A'-'Z'), digits ('0'-'9'), spaces, and the characters ".,;:?!". Quotes and parentheses in all cases are for clarity only.
-No element of text will contain a string of non-space characters longer than width.


{"The quick brown fox jumps over the lazy dog. "}
{ "The",
 "jumps" }
Only one word can fit per line. Making two columns with this output looks like this:
"The",  "over",
"fox",  "dog.",
{"Such a preposterous use of !punctuation! !!!","Who would write ... something like this ???"}
{ "Such a",
 "write ...",
 "preposterous use",
 "something like",
 "of !punctuation!",
 "this ???",
 "!!! Who would" }
Remember not to treat punctuation marks any differently than other characters.
{" Forsaking monastic tradition,    twelve jovial",
"  friars gave up their vocation for a questionable",
"     existence on the flying trapeze.    "}
{ "Forsaking monastic",
 "vocation for a",
 "tradition, twelve jovial",
 "questionable existence on",
 "friars gave up their",
 "the flying trapeze." }
All leading, trailing, and extra whitespace is ignored.
{" "," "," "," "," "," "}
Returns: { }
{" I WONDER by my troth, what thou,",
"and I Did, till we lovd? were we",
"not weand till then? But suckd on",
"countrey pleasures, childishly? Or",
"snorted we in the seaven sleepers",
"den? Twas so; But this, all",
"pleasures fancies bee. If ever any",
"beauty I did see, Which I desird,",
"and got, twas but a dreame of",
"thee. And now good morrow to our",
"waking soules, Which watch not one",
"another out of feare; For love, all",
"love of other sights controules,",
"And makes one little roome, an",
"every where. Let seadiscoverers to",
"new worlds have gone, Let Maps to",
"other, worlds on worlds have",
"showne, Let us possesse one world,",
"each hath one, and is one. My face",
"in thine eye, thine in mine",
"appeares, And true plaine hearts",
"doe in the faces rest, Where can we",
"finde two better hemispheares",
"Without sharpe North, without",
"declining West? What ever dyes, was",
"not mixt equally; If our two loves",
"be one, or, thou and I Love so",
"alike, that none doe slacken, none",
"can die.",
"John Donne"}
{ "I WONDER by my troth, what thou, and I Did,",
 "seadiscoverers to new worlds have gone, Let",
 "till we lovd? were we not weand till then?",
 "Maps to other, worlds on worlds have showne,",
 "But suckd on countrey pleasures, childishly?",
 "Let us possesse one world, each hath one, and",
 "Or snorted we in the seaven sleepers den?",
 "is one. My face in thine eye, thine in mine",
 "Twas so; But this, all pleasures fancies bee.",
 "appeares, And true plaine hearts doe in the",
 "If ever any beauty I did see, Which I desird,",
 "faces rest, Where can we finde two better",
 "and got, twas but a dreame of thee. And now",
 "hemispheares Without sharpe North, without",
 "good morrow to our waking soules, Which watch",
 "declining West? What ever dyes, was not mixt",
 "not one another out of feare; For love, all",
 "equally; If our two loves be one, or, thou",
 "love of other sights controules, And makes",
 "and I Love so alike, that none doe slacken,",
 "one little roome, an every where. Let",
 "none can die. John Donne" }

Problem url:

Problem stats url:


Running Wild


lbackstrom , schveiguy

Problem categories:

String Manipulation