FastPrepWrap Text with Retained Boundary Spaces

Wrap Text with Retained Boundary Spaces

Atlassian logoAtlassian● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Given text and maxLen, split the words into ordered lines without breaking a word. Each line uses the largest consecutive word group whose letters and internal single spaces fit the maximum.

For this exercise, assume a nonfinal line retains exactly one trailing source space when its word group is shorter than maxLen. If the group already fills maxLen, omit that following space. The final line has no trailing space. Add no other padding.

For this exercise, assume nonempty words contain printable ASCII characters other than space, exactly one ASCII space separates words, and input has no leading or trailing space. Every word individually fits; preserve punctuation, casing and repeated words. Empty input returns an empty array.

Function

wrapText(text: String, maxLen: int) → String[]

Examples

Example 1

text = "Hey there. Hello world"maxLen = 10return = ["Hey there.","Hello ","world"]

The complete original source fixture is preserved, including the literal result "Hello " with one trailing space.

Example 2

text = "aa b ccc d"maxLen = 5return = ["aa b ","ccc d"]

This authored fixture retains one separator on a short nonfinal line, without adding any further padding.

Example 3

text = "Abcd e f"maxLen = 4return = ["Abcd","e f"]

This authored fixture suppresses the separator after an exactly full line and leaves the final line unpadded.

Constraints

  • For this exercise, assume 0 <= text.length <= 2000 and 1 <= maxLen <= 100.
  • Each word has length at most maxLen. Nonspace characters have ASCII codes from 33 through 126.
  • Nonempty input uses exactly one space between words and has no leading or trailing space.

More Atlassian problems

See Atlassian hiring insights
public String[] wrapText(String text, int maxLen) {
    // Write your code here.
    return new String[0];
}
text"Hey there. Hello world"
maxLen10
expected["Hey there.", "Hello ", "world"]
Checking account…