leetcodedaybyday - Beats 100% with C++ and Beats 100% with Python3
IntuitionThe problem involves determining the number of valid times that can be represented given a partially complete time string in the format "HH:MM". Each '
# **Intuition**
The problem involves determining the number of valid times that can be represented given a partially complete time string in the format "HH:MM". Each '?' can represent any valid digit for its respective position, and we need to consider all possibilities.

# **Approach**
1. **Analyze Each Character**:
   - The first two characters represent the hours (`HH`), and the last two characters represent the minutes (`MM`).
   - For each '?' in `HH` or `MM`, determine the possible range of values it can take based on its position and the value of the other digit.

2. **Count Possibilities for `HH`**:
   - If both `time[0]` and `time[1]` are '?', there are 24 possibilities (00 to 23).
   - If `time[0]` is '?':
     - If `time[1] < '4'`, `time[0]` can be 0, 1, or 2 (3 options).
     - Otherwise, `time[0]` can be 0 or 1 (2 options).
   - If `time[1]` is '?':
     - If `time[0] < '2'`, `time[1]` can take 0-9 (10 options).
     - Otherwise, `time[1]` can take 0-3 (4 options).

3. **Count Possibilities for `MM`**:
   - If `time[3]` is '?', it can take 0-5 (6 options).
   - If `time[4]` is '?', it can take 0-9 (10 options).

4. **Calculate Total Combinations**:
   - Multiply all the possibilities derived from the above analysis.

# **Complexity**
- **Time Complexity**:  
  \(O(1)\), as the solution only involves a constant number of conditional checks.

- **Space Complexity**:  
  \(O(1)\), as no additional space is used apart from a few variables.
---

# Code
```cpp []
class Solution {
public:
    int countTime(string time) {
        int ans = 1;
        if (time[0] == '?' && time[1] == '?'){
            ans *= 24;
        } else {
            if (time[0] == '?'){
                if (time[1] < '4'){
                    ans *= 3;
                } else {
                    ans *= 2;
                }
            } if (time[1] == '?'){
                if (time[0] < '2'){
                    ans *= 10;
                } else {
                    ans *= 4;
                }
            }
        }
        if (time[3] == '?'){
            ans *= 6;
        }
        if (time[4] == '?'){
            ans *= 10;
        }
        return ans;
    }
};

```
```python3 []
class Solution:
    def countTime(self, time: str) -> int:
        ans = 1
        if time[0] == "?" and time[1] == "?":
            ans *= 24
        else:
            if time[0] == "?":
                if (time[1] < "4"):
                    ans *= 3
                else:
                    ans *= 2
            if time[1] == "?":
                if time[0] < "2":
                    ans *= 10
                else:
                    ans *= 4
        if time[3] == "?":
            ans *= 6
        if time[4] == "?":
            ans *= 10
        return ans


```


['String', 'Enumeration', 'C++', 'Python3']