-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathNumberOfValidClockTimes.java
35 lines (27 loc) · 1.03 KB
/
NumberOfValidClockTimes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// https://leetcode.com/problems/number-of-valid-clock-times
// T: O(1)
// S: O(1)
public class NumberOfValidClockTimes {
private static final char QUESTION = '?';
public int countTime(String time) {
return numberOfValidHourStrings(time.substring(0, 2)) * numberOfValidMinuteStrings(time.substring(3));
}
private int numberOfValidMinuteStrings(String mm) {
final char first = mm.charAt(0), second = mm.charAt(1);
if (first == QUESTION && second == QUESTION) return 60;
if (first != QUESTION && second != QUESTION) return 1;
if (first != QUESTION) return 10;
return 6;
}
private int numberOfValidHourStrings(String hh) {
final char first = hh.charAt(0), second = hh.charAt(1);
if (first == QUESTION && second == QUESTION) return 24;
if (first != QUESTION && second != QUESTION) return 1;
if (first != QUESTION) {
if (first == '2') return 4;
return 10;
}
if (second > '3') return 2;
return 3;
}
}