Problem · String

Maximum Time

Learn this problem
EasyGoogleINTERNOA
See Google hiring insights

Problem statement

You are given a string that represents time in the format hh:mm. Some of the digits are blank (represented by ?). Fill in ? such that the time represented by this string is the maximum possible. Maximum time: 23:59, minimum time: 00:00. You can assume that input string is always valid.

Function

maximumTime(time: String) → String

Examples

Example 1

time = "?4:5?"return = "14:59"
The leading digit must be 1 because the fixed hour ones digit is 4. The last minute digit can be 9.

Example 2

time = "23:5?"return = "23:59"
The fixed hour is already maximal, and the final minute digit can be 9.

Example 3

time = "?2:22"return = "22:22"
The second hour digit is fixed as 2, so choosing 2 for the first digit gives the maximum valid hour, 22.

Example 4

time = "0?:??"return = "09:59"
With a fixed leading 0, the largest hour ones digit is 9; the largest minute is 59.

Example 5

time = "??:??"return = "23:59"
All digits are unknown, so the maximum valid 24-hour time is 23:59.

Constraints

  • time.length = 5 and time[2] = ':'.
  • Every other character is a decimal digit or ?.
  • At least one replacement of all question marks produces a valid time from 00:00 through 23:59.

More Google problems

drafts saved locally
public String maximumTime(String time) {
    // write your code here
}
time"?4:5?"
expected"14:59"
checking account