FastPrepValid Times on a Digital Clock
Problem · Array

Valid Times on a Digital Clock

Learn this problem
EasyToptal logoToptalCONTRACTOAONSITE INTERVIEW

Problem statement

You are given four decimal digits a, b, c, and d.

Arrange all four supplied digit occurrences into a 24-hour time HH:MM. A time is valid when 00 <= HH <= 23 and 00 <= MM <= 59. Leading zeroes are allowed.

Return the number of distinct valid times that can be formed. Each supplied digit occurrence must be used exactly once; equal arrangements caused by repeated digits count only once.

Function

solution(a: int, b: int, c: int, d: int) → int

Examples

Example 1

a = 1b = 2c = 3d = 4return = 10

The digits form ten distinct valid times: 12:34, 12:43, 13:24, 13:42, 14:23, 14:32, 21:34, 21:43, 23:14, and 23:41.

Example 2

a = 1b = 4c = 1d = 4return = 3

The distinct valid times are 11:44, 14:14, and 14:41. Repeated permutations of the equal digits do not add another time.

Example 3

a = 8b = 6c = 7d = 5return = 0

No arrangement can put a valid digit in the tens place of the hour while also forming valid minutes.

Constraints

  • 0 <= a, b, c, d <= 9
  • Each parameter represents one decimal digit occurrence.
drafts saved locally
public int solution(int a, int b, int c, int d) {
    // Write your code here.
}
a1
b2
c3
d4
expected10
checking account