FastPrepReflow Text with Balanced Hyphens

Reflow Text with Balanced Hyphens

Atlassian logoAtlassian● MediumFULLTIMEPHONE SCREEN

Updated

Problem statement

Read the words from the strings in text in their original order, then arrange them into new lines no wider than width. Never split a word. Select the largest consecutive group of words that fits when separated by one hyphen.

Distribute hyphens between words as evenly as possible to reach the width on each fully justified line. Gap lengths may differ by at most one; put any extra separator characters in the leftmost gaps.

Leave the final line without padding, with one hyphen between its words. In this exercise, also leave every line containing only one word without padding, even if it is not the final line.

For this exercise, assume input contains only English letters and ASCII spaces. Ignore leading, trailing and repeated spaces inside source strings; source-string boundaries act as whitespace. An empty input or input without words returns an empty array.

Function

justifyHyphenText(text: String[], width: int) → String[]

Examples

Example 1

text = ["The day began as still as the","night abruptly lighted with","brilliant flame"]width = 24return = ["The--day--began-as-still","as--the--night--abruptly","lighted--with--brilliant","flame"]

With width 24, all three justified strings are exactly 24 characters and flame is unchanged.

Example 2

text = ["a bb ccc d e f"]width = 6return = ["a---bb","ccc--d","e-f"]

This example leaves the final multiword line e-f unpadded.

Example 3

text = ["abcdefg a b"]width = 8return = ["abcdefg","a-b"]

This example shows the rule that a nonfinal line with a single word is not padded.

Constraints

  • For this exercise, assume 0 <= text.length <= 100 and the total number of input characters is at most 2000.
  • For this exercise, assume 1 <= width <= 100, and every nonempty word has length at most width.
  • Each input string contains only uppercase or lowercase English letters and ASCII spaces.

More Atlassian Phone Screen questions

Similar questions at other companies

Atlassian interview experiences

All Atlassian interview questionsSee Atlassian hiring insights
Saved
class Solution {
    public String[] justifyHyphenText(String[] text, int width) {
        // Write your code here.
        return new String[0];
    }
}
Checking account…