Start learning free

LESSON 270 OF 349 · BACKTRACKING

Generate Binary Strings With K Ones in Swift

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: [
    "1100",
    "1010",
    "1001",
    "0110",
    "0101",
    "0011"
]
Notes: K <= N <= 20
STEP 1 OF 11
    Ctrl + Enter to check

    Generate Binary Strings With K Ones in Swift

    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free