Skip to content

Commit 6329d0d

Browse files
solves #2437: Number of Valid Clock Times in java
1 parent 879fc2e commit 6329d0d

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@
772772
| 2423 | [Remove Letter To Equalize Frequency](https://leetcode.com/problems/remove-letter-to-equalize-frequency) | [![Java](assets/java.png)](src/RemoveLetterToEqualizeFrequency.java) | |
773773
| 2427 | [Number of Common Factors](https://leetcode.com/problems/number-of-common-factors) | [![Java](assets/java.png)](src/NumberOfCommonFactors.java) | |
774774
| 2432 | [The Employee That Worked on the Longest Task](https://leetcode.com/problems/the-employee-that-worked-on-the-longest-task) | [![Java](assets/java.png)](src/TheEmployeeThatWorkedOnTheLongestTask.java) | |
775-
| 2437 | [Number of Valid Clock Times](https://leetcode.com/problems/number-of-valid-clock-times) | | |
775+
| 2437 | [Number of Valid Clock Times](https://leetcode.com/problems/number-of-valid-clock-times) | [![Java](assets/java.png)](src/NumberOfValidClockTimes.java) | |
776776
| 2441 | [Largest Positive Integer That Exists With Its Negative](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative) | | |
777777
| 2446 | [Determine if Two Events Have Conflict](https://leetcode.com/problems/determine-if-two-events-have-conflict) | | |
778778
| 2451 | [Odd String Difference](https://leetcode.com/problems/odd-string-difference) | | |

src/NumberOfValidClockTimes.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// https://leetcode.com/problems/number-of-valid-clock-times
2+
// T: O(1)
3+
// S: O(1)
4+
5+
public class NumberOfValidClockTimes {
6+
private static final char QUESTION = '?';
7+
8+
public int countTime(String time) {
9+
return numberOfValidHourStrings(time.substring(0, 2)) * numberOfValidMinuteStrings(time.substring(3));
10+
}
11+
12+
private int numberOfValidMinuteStrings(String mm) {
13+
final char first = mm.charAt(0), second = mm.charAt(1);
14+
15+
if (first == QUESTION && second == QUESTION) return 60;
16+
if (first != QUESTION && second != QUESTION) return 1;
17+
if (first != QUESTION) return 10;
18+
return 6;
19+
}
20+
21+
private int numberOfValidHourStrings(String hh) {
22+
final char first = hh.charAt(0), second = hh.charAt(1);
23+
24+
if (first == QUESTION && second == QUESTION) return 24;
25+
if (first != QUESTION && second != QUESTION) return 1;
26+
27+
if (first != QUESTION) {
28+
if (first == '2') return 4;
29+
return 10;
30+
}
31+
32+
if (second > '3') return 2;
33+
return 3;
34+
}
35+
}

0 commit comments

Comments
 (0)