A chain of unmanned weather relay stations transmits its callsign as a raw string of digits, but the aging transmitter hardware pads the signal with stray spaces and dashes inserted at arbitrary points along the way. Before a callsign can be logged, ground control strips out every space and every dash, keeping only the digits in their original left-to-right order, and then reflows those digits into clean blocks joined by single dashes. Blocks are built greedily from the front: while more than four digits remain unassigned, peel off the next three digits as one block. Once four or fewer digits remain, form the ending — if exactly four digits are left, split them into two blocks of two digits each; otherwise (two or three digits are left), the remaining digits form a single final block. Given a raw callsign, output its cleaned, reflowed form.
A single line containing the raw callsign S.
A single line containing the reflowed callsign: the digits of S, in their original order, grouped into blocks joined by single dashes according to the rule above.
Example 1
Input
20-543 012 3
Expected
205-430-123
Explanation
Removing the space and dash leaves the digit sequence 205430123 (9 digits). Since more than four digits remain, take the first three as a block ("205"); more than four still remain (6 left), so take the next three ("430"), leaving exactly three digits ("123"), which is not more than four, so they form the final block as-is. Joined with dashes: 205-430-123.
Example 2
Input
12 34
Expected
12-34
Explanation
Removing the space leaves the digit sequence 1234 (4 digits). Since 4 is not more than four, we go straight to the tail rule: exactly four digits remain, so they split into two blocks of two, "12" and "34". Joined with a dash: 12-34.
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 →