> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Binary Watch Python Solution with Tests

> Tested Python solution for LeetCode 401 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 401, [Easy](/catalog/easy). Topics: [Backtracking](/catalog/topics/backtracking), [Bit Manipulation](/catalog/topics/bit-manipulation). [View on LeetCode](https://leetcode.com/problems/binary-watch/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 401   # by problem number
lcpy gen -s binary_watch   # by problem name
```

## Problem

A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.

For example, the below binary watch reads `"4:51"`.

Given an integer `turnedOn` which represents the number of LEDs that are currently on (ignoring the PM), return *all possible times the watch could represent*. You may return the answer in **any order**.

The hour must not contain a leading zero.

* For example, `"01:00"` is not valid. It should be `"1:00"`.

The minute must consist of two digits and may contain a leading zero.

* For example, `"10:2"` is not valid. It should be `"10:02"`.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/04/08/binarywatch.jpg)

```
Input: turnedOn = 1
Output: ["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]
```

```
Input: turnedOn = 9
Output: []
```

### Constraints

* 0 \<= turnedOn \<= 10

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/binary_watch/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/binary_watch/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(12 * 60)
    # Space: O(1) excluding the output
    def read_binary_watch(self, turned_on: int) -> list[str]:
        if turned_on > 9:
            return []
        result: list[str] = []
        for hour in range(12):
            for minute in range(60):
                if hour.bit_count() + minute.bit_count() == turned_on:
                    result.append(f"{hour}:{minute:02d}")
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(12 \* 60) | O(1) excluding the output |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.