Problem · Array

Enumerate Valid Clock Times

Learn this problem
EasyMicrosoft logoMicrosoftFULLTIMEOA
See Microsoft hiring insights

Problem statement

Given exactly four decimal digits in digits, enumerate every valid 24-hour time that can be formed by using each supplied occurrence exactly once.

A valid time has format HH:MM, where 00 <= HH <= 23 and 00 <= MM <= 59. Preserve leading zeroes, remove duplicate times caused by equal digits, and return the times in chronological order.

Function

enumerateValidTimes(digits: int[]) → String[]

Examples

Example 1

digits = [1,2,3,4]return = ["12:34","12:43","13:24","13:42","14:23","14:32","21:34","21:43","23:14","23:41"]

These are exactly the valid chronological arrangements; arrangements such as 31:24 are not valid 24-hour times.

Example 2

digits = [0,0,1,2]return = ["00:12","00:21","01:02","01:20","02:01","02:10","10:02","10:20","12:00","20:01","20:10","21:00"]

The two zero occurrences are both used, and duplicate permutations produce only one copy of each time.

Example 3

digits = [5,5,5,5]return = []

No arrangement has a valid hour.

Constraints

  • digits.length == 4.
  • 0 <= digits[i] <= 9.

More Microsoft problems

drafts saved locally
public String[] enumerateValidTimes(int[] digits) {
    // Write your code here.
}
digits[1,2,3,4]
expected["12:34", "12:43", "13:24", "13:42", "14:23", "14:32", "21:34", "21:43", "23:14", "23:41"]
checking account