Number of Valid Clock Times — LeetCode 2437 Python Solution
- Problem
- #2437
- Pattern
- Hash Map
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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".
Example
- 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.
Python solution
class Solution:
def countTime(self, time: str) -> int:
def check(s: str, t: str) -> bool:
return all(a == b or b == '?' for a, b in zip(s, t))
return sum(
check(f'{h:02d}:{m:02d}', time) for h in range(24) for m in range(60)
)Complexity
| Measure | Complexity |
|---|---|
| Time | O(24 \times 60) |
| Space | O(1) auxiliary |
Pattern: Hash Map
Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 2437. Number of Valid Clock Times is filed here because the reference solution below belongs to the algorithm family this hub collects, even though its LeetCode tags point elsewhere.
The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 2437. Number of Valid Clock Times?
- LeetCode 2437. Number of Valid Clock Times is rated Easy on LeetCode.
- What is the time complexity of LeetCode 2437. Number of Valid Clock Times?
- The Python solution on this page runs in O(24 \times 60).
- What is the space complexity of LeetCode 2437. Number of Valid Clock Times?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 2437. Number of Valid Clock Times cover?
- LeetCode 2437. Number of Valid Clock Times is tagged String and Enumeration on LeetCode.