Problem Statement
You are going to write code to layout text on a document. There are certain parameters that will determine the look of the document. These include width, the number of characters that can fit on each line, and space, the number of space characters (' ') between each word. You will also be given a list of words that must be written on the document.
To layout the document you will start at the first line, and keep adding words until you cannot fit any more on the line. Then you move on to the next line. There are no leading or trailing spaces on any of the lines. Words cannot be broken up between multiple lines. A line can never have more than width characters, and there will be no empty lines. In addition, the words must be added in the order they occur in the input. For example:
To layout the document you will start at the first line, and keep adding words until you cannot fit any more on the line. Then you move on to the next line. There are no leading or trailing spaces on any of the lines. Words cannot be broken up between multiple lines. A line can never have more than width characters, and there will be no empty lines. In addition, the words must be added in the order they occur in the input. For example:
words = {"la","di","da","di","c","hmm"}
space = 3
width = 11
Return value = {"la di",
"da di c",
"hmm"}
Initially "la" was placed on the first line. 3 spaces later "di" was added. "da" would not fit 3 spaces after "di" so it was added to the next line. Continuing this process the return value was formed. Given words, space, and width you will return a String[] representing the laid out document, where each element represents one of the lines.Definition
- Class:
- TextLayout
- Method:
- layout
- Parameters:
- String[], int, int
- Returns:
- String[]
- Method signature:
- String[] layout(String[] words, int space, int width)
- (be sure your method is public)
Constraints
- words will contain between 1 and 50 elements inclusive.
- Each element of words will contain only lowercase and uppercase letters (a-zA-z)
- Each element of words will contain between 1 and 50 characters inclusive
- space will be between 0 and 10 inclusive
- width will be between k and 50 inclusive where k is the length of the longest element of words
Examples
{"la","di","da","di","c","hmm"}3
11
Returns: { "la di", "da di c", "hmm" }Example from above{"aaaaa","aaaaa","aaaaa","aaaaa"}5
14
Returns: { "aaaaa", "aaaaa", "aaaaa", "aaaaa" }Due to the amount of space between words, only one word can fit on each line.{"H","e","l","l","o"}0
5
Returns: { "Hello" }space can be 0{"Hmm","Hmm","Hmm","Hmm", "Hmm","Hmm","Hmm","Hmm"}1
15
Returns: { "Hmm Hmm Hmm Hmm", "Hmm Hmm Hmm Hmm" }Perfect fit for 2 lines{"Blah"}10
4
Returns: { "Blah" }Just one word