An old arcade cabinet's scoreboard has a hardware quirk: every digit display can only physically show a 6 or a 9, since the two digits are just flipped versions of the same segment pattern. A player's current score is shown using only these two digits. The technician may perform at most one repair operation: pick a single displayed digit that currently reads 6 and physically flip it so it reads 9 (flipping a 9 back to a 6 would only ever decrease the score, so it's never worth doing). Determine the maximum score obtainable after performing at most one such flip.
A single line containing the score as a string of digits, where every digit is either 6 or 9.
The maximum possible score after at most one flip, printed as an integer with no leading zeros.
1 <= number of digits <= 4 Each digit is 6 or 9.
Example 1
Input
9669
Expected
9969
Explanation
The digits are 9, 6, 6, 9. The leftmost 6 is at position 2; flipping it to 9 gives 9969, which is larger than flipping the 6 at position 3 (which would give 9699). Maximum score is 9969.
Example 2
Input
9996
Expected
9999
Explanation
There is only one 6, at the last position. Flipping it gives 9999, the maximum possible score.
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 →