Given list of 6 digits, return the earliest time that can be formed from those digits. If no legal time can be constructed, return -1.
Example [1, 2, 3, 4, 5, 6]
output: 12:34:56
Example [1, 2, 3, 5, 8, 9]
output: 12:38:59
Example [1, 2, 3, 7, 8, 9]
output: 17:28:39
Example [6, 7, 8, 9, 9, 9]
output: -1
I started by sorting the list. I am not sure how we can detect the case in the second example, to swap the 8 and 5. What algorithm can I use to solve this?