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 recursive approach builds combinations by including or excluding each character. The function iterates over each character in the string, appending it to a prefix and recursively continues with the remaining characters until all combinations are generated.

Key Steps:

  • Initialize an empty results array
  • Use a helper function that takes a prefix and remaining characters
  • For each character, create two branches: include it or skip it

Example

function getAllCombinations(str) {
    let results = [];

    function combine(prefix, remaining) {
        if (prefix) {
            results.push(prefix); // Add the current combination
        }
        for (let i = 0; i 

["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. For a string of length n, we iterate through all numbers from 1 to 2^n - 1 to generate subsets.

How it works:

  • Each bit position represents whether to include a character
  • Binary 101 for "abc" means include 'a' and 'c', skip 'b'
  • We check each bit using bitwise AND operation

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 

["a", "b", "ab", "c", "ac", "bc", "abc"]

Comparison

Feature Recursive Approach Iterative Approach (Bitmasking)
Time Complexity O(2^n) O(n × 2^n)
Space Complexity O(n) call stack O(1) auxiliary space
Readability More intuitive for recursion fans Requires bitwise operation knowledge
Order of Results Lexicographic order Binary counting order

Applications

  • Cryptography: Analyzing potential subsets of encrypted data
  • Data Science: Generating all subsets of a dataset for analysis
  • Algorithm Testing: Creating test cases for problems involving permutations or combinations
  • Game Development: Generating possible moves or item combinations

Conclusion

Both approaches effectively generate all possible combinations of a string's characters. The recursive approach is more intuitive but uses call stack space, while bitmasking is more memory-efficient but requires understanding of bitwise operations.

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

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements