
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate All Combinations of Supplied Words in JavaScript
In JavaScript, there are scenarios where you may need to generate every possible combination of a string's characters. This can be especially useful in areas like cryptography, analyzing subsets of data, or solving problems involving permutations. In this article, we'll learn to implement a JavaScript function to achieve this task.
Generate all combinations of supplied words
Following are the different approaches for generating all combinations of supplied words ?
Using Recursive Approach
The function iterates over each character in the string, appending it to a prefix. It recursively continues with the remaining characters until all combinations are generated.
Initialize output in results array:
let results = [];Recursive Function Call:
combine("", str);Prefix is the current combination being built, and .slice()returns the substring of the remaining characters:
combine(prefix + remaining[i], remaining.slice(i + 1));
Example
function getAllCombinations(str) { let results = []; function combine(prefix, remaining) { if (prefix) { results.push(prefix); // Add the current combination } for (let i = 0; i < remaining.length; i++) { combine(prefix + remaining[i], remaining.slice(i + 1)); // Recursive call } } combine("", str); // Start with an empty prefix return results; } const input = "abc"; console.log(getAllCombinations(input));
Output
["a", "ab", "abc", "ac", "b", "bc", "c"]
Iterative Approach Using Bitmasking
This method uses the concept of bitmasking, where each combination corresponds to a subset of characters represented by a binary number. Each combination is represented by a binary number, where each bit corresponds to whether a character is included in the subset. For a string of length n, we iterate through all numbers from 1 to 2^n - 1 to generate subsets.
Iterate through bitmask values using a for loop and continuously check if the conditions are met. :
for (let i = 1; i < (1 << n); i++) { }Checking Bit Status using Bitwise Operator and adding the string in the str array:
if (i & (1 << j)) combination += str[j];Adding Combination to Results using Arrays .push() method:
results.push(combination);
Example
function getAllCombinations(str) { let results = []; let n = str.length; // Generate all combinations by iterating through 2^n possibilities // Start at 1 to avoid empty combination for (let i = 1; i < (1 << n); i++) { let combination = ""; for (let j = 0; j < n; j++) { // Check if the j-th bit in i is set if (i & (1 << j)) { combination += str[j]; } } results.push(combination); } return results; } const input = "abc"; console.log(getAllCombinations(input));
Output
["a", "ab", "abc", "ac", "b", "bc", "c"]
Applications
-
Cryptography: Analyzing potential subsets of encrypted data.
-
Data Science: Generating all subsets of a dataset.
- Algorithm Testing: Creating test cases for problems involving permutations or combinations.
Comparison Table
Feature | Recursive Approach | Iterative Approach (Bitmasking) |
Concept | Builds combinations by recursively appending characters. |
It uses binary representation to generate subsets. |
Complexity |
It is easier to understand for those familiar with recursion. |
Slightly easier to grasp with knowledge of bitwise operations. |
Conclusion
Both the Recursive Approach and Iterative Approach (Bitmasking) effectively generate all possible combinations of a string's characters. The choice between them depends on the specific use case, ease of understanding, and efficiency needs.