← Questions

Count valid 24-hour times from four digits

0
MicrosoftCoding

Write a function count_valid_times(a, b, c, d) that takes four digits (each an integer from 0 to 9).

Arrange all four digits as HH:MM on a 24-hour clock, using each given digit exactly once. A time is valid when the hour is from 00 to 23 and the minute is from 00 to 59. Leading zeros are allowed.

Return the number of distinct valid times. If the digits repeat, two different arrangements may show the same time; count that time once.

Constraints

  • 0 <= a, b, c, d <= 9

Example 1

Input:  a = 1, b = 8, c = 3, d = 2
Output: 6

The valid times are 12:38, 13:28, 18:23, 18:32, 21:38 and 23:18.

Example 2

Input:  a = 2, b = 3, c = 3, d = 2
Output: 3

The valid times are 22:33, 23:23 and 23:32. Several orderings of the digits produce each of these, but each time counts once.

Example 3

Input:  a = 6, b = 2, c = 4, d = 7
Output: 0

Any hour that starts with 2 would be 24 or more, and there is no 0 or 1 to start an hour with.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding