FastPrepNext Time Using the Same Digits

Next Time Using the Same Digits

Google logoGoogle● MediumINTERNONSITE INTERVIEW
Learn

Problem statement

You are given a valid 24-hour time time in zero-padded "HH:MM" format.

Find the next valid time reached strictly after time that uses exactly the same four digits with the same multiplicities. The colon is not a digit. Time advances cyclically, so the search continues into the next day when necessary.

Return the next time in zero-padded "HH:MM" format. If no different arrangement of the digits forms a valid time, return time, representing the same clock time on the next day.

Function

nextTimeUsingSameDigits(time: String) → String

Examples

Example 1

time = "09:25"return = "09:52"

09:52 is the earliest later valid time made from exactly the digits 0, 9, 2, 5.

Example 2

time = "12:34"return = "12:43"

Swapping the minute digits gives the earliest valid later time using the same digit multiset.

Example 3

time = "11:11"return = "11:11"

No different arrangement exists, so the next matching time is 11:11 on the following day.

Constraints

  • time.length == 5.
  • time[2] == ':'.
  • time is a valid zero-padded time from 00:00 through 23:59.

More Google problems

See Google hiring insights
public String nextTimeUsingSameDigits(String time) {
    // Write your code here.
}
time"09:25"
expected"09:52"
Checking account…