Forming string using 0 and 1 in JavaScript

We need to write a JavaScript function that takes an array of binary strings and determines the maximum number of strings that can be formed using at most m zeros and n ones.

Problem Statement

Given an array of strings containing only '0' and '1', find how many strings can be selected such that the total count of zeros doesn't exceed m and the total count of ones doesn't exceed n.

For example, with the array ["10", "0001", "111001", "1", "0"] and limits m=5 zeros and n=3 ones:

Input: arr = ["10", "0001", "111001", "1", "0"], m = 5, n = 3
Output: 4

Solution Approach

This is a classic dynamic programming problem similar to the 0/1 knapsack problem. We use a 2D DP table where dp[i][j] represents the maximum number of strings we can form with i zeros and j ones.

Helper Function: Count Zeros and Ones

const getCount = (str) => {
    let zeros = 0, ones = 0;
    for (let char of str) {
        if (char === '0') zeros++;
        else ones++;
    }
    return { zeros, ones };
};

// Test the helper function
console.log(getCount("10"));      // { zeros: 1, ones: 1 }
console.log(getCount("0001"));    // { zeros: 3, ones: 1 }
console.log(getCount("111001"));  // { zeros: 2, ones: 4 }
{ zeros: 1, ones: 1 }
{ zeros: 3, ones: 1 }
{ zeros: 2, ones: 4 }

Complete Solution

const findAllStrings = (arr, m, n) => {
    // Helper function to count zeros and ones in a string
    const getCount = str => {
        let zeros = 0, ones = 0;
        for (let char of str) {
            char === '0' ? zeros++ : ones++;
        }
        return { zeros, ones };
    };
    
    // Create DP table: dp[i][j] = max strings with i zeros and j ones
    const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
    
    // Process each string
    for (let i = 0; i = zeros; j--) {
            for (let k = n; k >= ones; k--) {
                // Either include current string or don't
                dp[j][k] = Math.max(dp[j - zeros][k - ones] + 1, dp[j][k]);
            }
        }
    }
    
    return dp[m][n];
};

// Test the function
const arr = ["10", "0001", "111001", "1", "0"];
const m = 5, n = 3;
console.log(findAllStrings(arr, m, n));
4

How It Works

The algorithm works by:

  1. Count characters: For each string, count the number of zeros and ones
  2. Dynamic Programming: Build a table where dp[i][j] stores the maximum strings possible with i zeros and j ones
  3. Backward traversal: Process the DP table backwards to ensure each string is considered only once
  4. Optimal choice: For each string, decide whether including it gives a better result

Step-by-Step Example

For our example with strings ["10", "0001", "111001", "1", "0"] and limits m=5, n=3:

  • "10": 1 zero, 1 one ? (can include)
  • "0001": 3 zeros, 1 one ? (can include)
  • "111001": 2 zeros, 4 ones ? (exceeds n=3 ones)
  • "1": 0 zeros, 1 one ? (can include)
  • "0": 1 zero, 0 ones ? (can include)

The optimal selection is: "10", "0001", "1", "0" using 5 zeros and 3 ones exactly.

Conclusion

This dynamic programming solution efficiently finds the maximum number of binary strings that can be formed within given constraints. The time complexity is O(arr.length × m × n) and space complexity is O(m × n).

Updated on: 2026-03-15T23:19:00+05:30

311 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements