A warehouse conveyor sensor logs the crates that pass a checkpoint as a single line of text: each crate is printed as a run of lowercase letters, and the gaps the sensor recorded between crates (including any gap before the first crate or after the last one) are printed as runs of one or more space characters. The sensor's clock sometimes drifts, so the recorded gaps are not evenly sized. You must rebalance the log so that every gap between two consecutive crates uses exactly the same number of spaces (as large as possible), and any spaces left over after that equal split are appended after the last crate, with no spaces left before the first crate.
If the log records only a single crate, all of its recorded spaces (wherever they originally appeared) must appear after that crate in the rebalanced log.
A single line containing the log s.
A single line: the rebalanced log, which has the same length as s.
1 <= length of s <= 500s consists only of lowercase English letters (a-z) and the space character.s contains at least one crate (a maximal run of letters).s may additionally have spaces before the first crate and/or after the last crate.Example 1
Input
load crates fast
Expected
load crates fast
Explanation
The log has 3 crates (load, crates, fast) and 9 total space characters (2 before load, 3 between load and crates, 1 between crates and fast, 3 after fast). With 3 crates there are 2 gaps between crates, so the 9 spaces split evenly give 4 spaces per gap with 1 left over; the leftover space is appended after the last crate, giving `load` + 4 spaces + `crates` + 4 spaces + `fast` + 1 trailing space.
Example 2
Input
warehouse
Expected
warehouse
Explanation
There is only a single crate, `warehouse`, so every recorded space (3 before it and 2 after it, 5 in total) must move to appear after the crate, giving `warehouse` followed by 5 spaces.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →