JavaScript Program to Generate all Binary Strings From Given Pattern Last Updated : 06 Jun, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we are going to learn about Generating all binary strings from a given pattern in JavaScript. Generating all binary strings from a given pattern involves creating a set of binary sequences that follow the pattern's structure, where specific positions in the sequences can be filled with '0' or '1' based on the pattern's placeholders or rules. Table of Content Using RecursionUsing QueueUsing bitwise operationsWe will explore all the above methods along with their basic implementation with the help of examples. Using RecursionIn this approach, Recursion involves a function calling itself with modified parameters.In this example, recursion generates binary strings based on the input pattern.It branches at wildcard characters ('X') and preserves non-'X' characters, exploring all valid combinations.Example: In this example, The BinaryStrings function generates binary strings by replacing 'X' in the input string 'X1X' with '0' and '1', recursively exploring all possibilities and printing the results. JavaScript function BinaryStrings(str, index = '') { index.length === str.length ? console.log(index) : str[index.length] === 'X' ? (BinaryStrings(str, index + '0'), BinaryStrings(str, index + '1')) : BinaryStrings(str, index + str[index.length]); } const result = 'X1X'; BinaryStrings(result); Output010 011 110 111 Using QueueIn this approach, We use a queue to iteratively generate binary strings from the 'X' pattern.Expands the queue by appending '0' and '1' for 'X' characters or preserving non-'X' characters.Prints valid binary strings when they reach the pattern's length.Example: In this example, we are using the above-explained approach. JavaScript function generateBinaryStrings(str) { const queue = ['']; for (let i = 0; i < queue.length; i++) { const current = queue[i]; if (current.length === str.length) { console.log(current); } else { const nextIndex = current.length; if (str[nextIndex] === 'X') { queue.push(current + '0'); queue.push(current + '1'); } else { queue.push(current + str[nextIndex]); } } } } const result = 'X1X'; generateBinaryStrings(result); Output010 011 110 111 Using bitwise operationsIn this approach, we first count the number of wildcard characters ('X') in the pattern. Then, we iterate from 0 to 2^wildcardCount - 1, generating all possible binary numbers within that range. For each binary number, we replace the wildcard characters in the pattern with the corresponding binary digits and print the resulting binary string. Example: JavaScript function generateBinaryStrings(str) { // Convert the input pattern into an array for easier manipulation let pattern = str.split(''); // Find the total number of wildcard characters ('X') in the pattern let wildcardCount = pattern.reduce((count, char) => char === 'X' ? count + 1 : count, 0); // Generate all possible binary numbers from 0 to 2^wildcardCount - 1 for (let i = 0; i < Math.pow(2, wildcardCount); i++) { // Convert the number to binary representation let binary = i.toString(2).padStart(wildcardCount, '0'); // Replace wildcard characters ('X') with binary digits let result = pattern.map(char => char === 'X' ? binary.charAt(0) : char).join(''); // Print the generated binary string console.log(result); } } const pattern = 'X1X'; generateBinaryStrings(pattern); Output010 010 111 111 Comment More infoAdvertise with us Next Article JavaScript Program to Generate all Binary Strings From Given Pattern P parzival_op Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League javascript-string JavaScript-DSA JavaScript-Program Geeks Premier League 2023 +3 More Similar Reads JavaScript Program to Generate all Binary Strings Without Consecutive 1âs Given an integer, K. Generate all binary strings of size k without consecutive 1âs. Examples: Input : K = 3 Output : 000 , 001 , 010 , 100 , 101 Input : K = 4 Output: 0000 0001 0010 0100 0101 1000 1001 1010Table of Content Approach 1: Using Recursive FunctionApproach 2: Using Stack Data StructureApp 4 min read JavaScript Program to FindNumber of Flips to make Binary String Alternate In this problem, we aim to determine the minimum number of flips needed to transform a binary string into an alternating sequence of '0's and '1's. A flip refers to changing a '0' to '1' or a '1' to '0'. The objective is to find the most efficient way to achieve this alternating pattern.Examples:Inp 4 min read JavaScript Program to Check if all Bits can be made Same by Single Flip In this article, we will explore how to determine if it's possible to make all bits the same in a binary string by performing a single flip operation. We will cover various approaches to implement this in JavaScript and provide code examples for each approach.Examples:Input: 1101Output: YesExplanati 5 min read JavaScript Program to Print all Subsequences of a String A subsequence is a sequence that can be derived from another sequence by deleting zero or more elements without changing the order of the remaining elements. Subsequences of a string can be found with different methods here, we are using the Recursion method, Iteration method and Bit manipulation me 3 min read Java Program to Implement Bitap Algorithm for String Matching The Bitap Algorithm is an approximate string matching algorithm. The algorithm tells whether a given text contains a substring which is "approximately equal" to a given pattern. Here approximately equal states that if the substring and pattern are within a given distance k of each other. The algorit 5 min read Java Program to Convert Octal to Binary Given an Octal number as input, the task is to convert that number into its Binary equivalent number. Example: Input: Octal Number = 513 Output: Binary equivalent value is: 101001011 Explanation : Binary equivalent value of 5: 101 Binary equivalent value of 1: 001 Binary equivalent value of 3: 011Oc 5 min read Java Program to Print Alphabet Inverted Heart Pattern Alphabet Inverted Heart Pattern consists of two parts. The upper part of the pattern is a triangle. The base of the pattern has two peaks and a gap between them. Hence, the desired pattern looks like as shown in the illustration. Illustration: A BBB CCCCC DDDDDDD EEEEEEEEE FFFFFFFFFFF GGGGGGGGGGGGG 4 min read Java Program to Convert Binary Code into Gray Code Without Using Recursion Binary Code of a number is the representation of a number in Binary (base-2) number system. In Binary Number System, each number is expressed using only two literals (0 and 1). Each of these literals is called a bit. The binary number system is very useful in digital electronic circuits. Gray Code o 4 min read Check If it is Possible to Convert Binary String into Unary String Given a binary string S of length N. You can apply following operation on S any number of times. Choose two adjacent characters, such that both are 1's or 0's. Then invert them, Formally, if both are 0's then into 1's or vice-versa. Then your task is to output YES or NO, by checking that using given 6 min read Count Number of Distinct Binary Strings After Applying Flip Operations Given a binary string s and a positive integer k. In a operation you can repeatedly choose any substring of length k in s and flip all its characters (0s to 1s, 1s to 0s). The task is to return the number of distinct strings that can be obtained, modulo 10^9 + 7. You can perform the flip operation a 5 min read Like