Description
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".
Example 1:
Input: turnedOn = 1 Output: ["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]
Example 2:
Input: turnedOn = 9 Output: []
Constraints:
0 <= turnedOn <= 10
Solutions
This code solves the Binary Watch problem by finding all valid times that can be displayed when exactly turnedOn LEDs are lit on a 10-LED binary watch. The first 4 LEDs represent hours (with place values 8, 4, 2, 1), and the remaining 6 LEDs represent minutes (with place values 32, 16, 8, 4, 2, 1). The dfs() helper function uses depth-first search to recursively generate all possible 10-bit binary strings containing exactly turnedOn ones, building the string one bit at a time. For each complete 10-character string, the toTime() helper converts it into a time format by summing the place values of all lit (1) positions for hours and minutes separately. If the resulting hours are less than 12 and minutes are less than 60 (valid time bounds), the time is formatted as HH:MM (with zero-padding for minutes) and added to the result array. The function returns all valid times that match the constraint.
/**
* @param {number} turnedOn
* @return {string[]}
*/
var readBinaryWatch = function(turnedOn) {
const res = [];
const toTime = (s) => {
let hours = 0;
if (s[0] === '1') hours += 8;
if (s[1] === '1') hours += 4;
if (s[2] === '1') hours += 2;
if (s[3] === '1') hours += 1;
let minutes = 0;
if (s[4] === '1') minutes += 32;
if (s[5] === '1') minutes += 16;
if (s[6] === '1') minutes += 8;
if (s[7] === '1') minutes += 4;
if (s[8] === '1') minutes += 2;
if (s[9] === '1') minutes += 1;
if (hours >= 12 || minutes >= 60) {
return null;
}
return hours + ':' + minutes.toString().padStart(2, '0');
};
const dfs = (left, cur) => {
if (cur.length === 10) {
if (left === 0) {
const time = toTime(cur);
if (time) {
res.push(time);
}
}
return;
}
if (left > 0) {
dfs(left - 1, cur + '1');
}
dfs(left, cur + '0');
};
dfs(turnedOn, '');
return res;
};