
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Forming string using 0 and 1 in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of strings, arr, formed using 0 and 1 only as the first argument.
The function takes two numbers as the second and third argument, m and respectively. The task of our function is to find how many strings from the array arr can be formed using at most m 0s and n 1s.
For example, if the input to the function is −
const arr = ["10", "0001", "111001", "1", "0"]; const m = 5, n = 3;
Then the output should be −
const output = 4;
Output Explanation:
There are in total 4 strings that can be formed by the use of 5 0s and 3 1s, which are −
“10,”0001”,”1”,”0”
Example
The code for this will be −
const arr = ["10", "0001", "111001", "1", "0"]; const m = 5, n = 3; const findAllStrings = (arr = [], m = 1, n = 1) => { const getCount = str => str.split('').reduce((acc, cur) => { cur === '0' ? acc.zeros++ : acc.ones++; return acc; }, {zeros:0, ones:0}); const dp = Array.from({length: m+1}, () => Array(n+1).fill(0)); for(let i = 0; i < arr.length; i++) { const {zeros, ones} = getCount(arr[i]); for(let j = m; j >= zeros; j--) { for(let k = n; k >= ones; k--) { dp[j][k] = Math.max(dp[j-zeros][k-ones]+1, dp[j][k]); } } } return dp[m][n] }; console.log(findAllStrings(arr, m, n));
Output
And the output in the console will be −
4
- Related Articles
- Forming the nearest time using current time in JavaScript
- Forming palindrome using at most one deletion in JavaScript
- Forming and matching strings of an array based on a random string in JavaScript
- Contiguous subarray with 0 and 1 in JavaScript
- Find all the patterns of “1(0+)1” in a given string using Python Regex
- 0/1 Knapsack using Branch and Bound in C++
- Forming the longest word in JavaScript
- 0/1 Knapsack using Branch and Bound in C/C++?
- JavaScript Strings: Replacing i with 1 and o with 0
- Find all the patterns of “1(0+)1” in a given string in C++
- Count of occurrences of a “1(0+)1” pattern in a string in C++
- Segregate 0’s and 1’s in an array list using Python?
- How to get a pseudo-random number between 0 and 1 in JavaScript?
- PHP $string{0} vs. $string[0];
- Sorting string alphabetically and inserting underscores using JavaScript

Advertisements