Start learning free

LESSON 411 OF 674 · BACKTRACKING

Generate Binary Strings With K Ones

Practice O(2^n)

Given two non-negative integers N and K, return the binary strings of length N with exactly K ones.

Example:

Input: N = 4, K = 2
Output: [
    "0011",
    "0101",
    "0110",
    "1001",
    "1010",
    "1100"
]
Notes: K <= N <= 20
STEP 1 OF 11
    Ctrl + Enter to check