Bit Strings By Popcount
EasyPython 3Bit manipulationSorting
Given an integer k (the number of bits), return every length-k binary string, ordered first by the number of set bits (ascending) and, within the same number of set bits, by numeric value (ascending). The result includes the all-zeros string and the all-ones string.
Constraints: - k >= 1 - The result always contains exactly 2**k strings. - Each string has length k, left-padded with zeros.
Examples:
k = 1 -> ['0', '1']
k = 2 -> ['00', '01', '10', '11']
k = 3 -> ['000', '001', '010', '100', '011', '101', '110', '111']
(0 bits, then 1 bit, then 2 bits, then 3 bits set)Python 3, run in your browser. Run tests runs the problem's own tests with a free account; the AI interviewer reads your code and your runs in a practice interview.
Loading the editor…