g2401_2500.s2437_number_of_valid_clock_times.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g2401_2500.s2437_number_of_valid_clock_times;
// #Easy #String #Enumeration #2022_12_07_Time_0_ms_(100.00%)_Space_42.2_MB_(29.58%)
/**
* 2437 - Number of Valid Clock Times\.
*
* Easy
*
* You are given a string of length `5` called `time`, representing the current time on a digital clock in the format `"hh:mm"`. The **earliest** possible time is `"00:00"` and the **latest** possible time is `"23:59"`.
*
* In the string `time`, the digits represented by the `?` symbol are **unknown** , and must be **replaced** with a digit from `0` to `9`.
*
* Return _an integer_ `answer`_, the number of valid clock times that can be created by replacing every_ `?`_with a digit from_ `0` _to_ `9`.
*
* **Example 1:**
*
* **Input:** time = "?5:00"
*
* **Output:** 2
*
* **Explanation:** We can replace the ? with either a 0 or 1, producing "05:00" or "15:00". Note that we cannot replace it with a 2, since the time "25:00" is invalid. In total, we have two choices.
*
* **Example 2:**
*
* **Input:** time = "0?:0?"
*
* **Output:** 100
*
* **Explanation:** Each ? can be replaced by any digit from 0 to 9, so we have 100 total choices.
*
* **Example 3:**
*
* **Input:** time = "??:??"
*
* **Output:** 1440
*
* **Explanation:** There are 24 possible choices for the hours, and 60 possible choices for the minutes. In total, we have 24 \* 60 = 1440 choices.
*
* **Constraints:**
*
* * `time` is a valid string of length `5` in the format `"hh:mm"`.
* * `"00" <= hh <= "23"`
* * `"00" <= mm <= "59"`
* * Some of the digits might be replaced with `'?'` and need to be replaced with digits from `0` to `9`.
**/
public class Solution {
public int countTime(String time) {
int[] counts = new int[] {3, 10, 0, 6, 10};
char[] ch = time.toCharArray();
int result = 1;
if (ch[0] == '2') {
counts[1] = 4;
}
if ((ch[1] - '0') > 3) {
counts[0] = 2;
}
if (ch[0] == '?' && ch[1] == '?') {
counts[0] = 1;
counts[1] = 24;
}
for (int i = 0; i < 5; i++) {
char ch1 = ch[i];
if (ch1 == '?') {
result *= counts[i];
}
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy