Generating all combinations of a string in JavaScript means finding every possible arrangement or subset of characters from a given string. It is commonly used in recursion, backtracking, and problem-solving tasks.
- Uses recursion or iterative methods.
- Helpful in permutations and subset problems.
- Time complexity grows exponentially.
- Used in algorithm practice and coding interviews.
- Applicable in password generation or pattern matching.
Example:
Dog => Possible Combination [D], [Do], [Dog], [o], [og], [g]Following are the several approaches to generate all the combinations of a string in JavaScript:
Approach 1: Use .push() and .slice() method
- In this approach, we will use the data structure called an array and will run two for loops on the given string which is the main logical part of our code
- Further, we will use .push() and .slice() methods to add our result to an array.
let possibleCombinations = (str) => {
let combinations = [];
for (let i = 0; i < str.length; i++) {
for (let j = i + 1; j < str.length + 1; j++) {
combinations.push(str.slice(i, j));
}
}
return combinations;
}
console.log(possibleCombinations('Dog'));
Approach 2: Using the .charAt() and .concat() method
- In this approach, we will again be using an array as our result printing variable and we will take our current index value as starting value (which is 0).
- Then we will run a while loop and inside that while loop we will store our character present at our current index value using the .charAt() method.
- Then we will declare a temporary array in which we will store that character obtained.
- Then we will run a for-in loop where in we will push our result and thereafter we will add our result in the result variable using the .concat() method and we will then increment our current index variable value.
let stringCombinations = (str) => {
let strLength = str.length;
let result = [];
let currentIndex = 0;
while (currentIndex < strLength) {
let char = str.charAt(currentIndex);
let x;
let arrTemp = [char];
for (x in result) {
arrTemp.push("" + result[x] + char);
}
result = result.concat(arrTemp);
currentIndex++;
}
return result;
};
console.log(stringCombinations("dog"));
Approach 3: Using for loop and .push() method
- In this approach, we will use two arrays one is temporary which will initially store our results and at the end, we will add our results in the second result array.
- Here we will use firstly a for loop and inside that, for loop, we will add each character of a string in the temporary array, and then we will take starting index value as 0.
- Then inside the for loop, we will use .push() method to push our result into the temporary array and increment the current index value.
- Then after the for loop, we will add the result into our result array from the temporary array and then print the result array.
let combinations = (str) => {
let tempArr = [];
let resultArr = [];
for (let i = 0; i < str.length; i++) {
tempArr = [str[i]];
let index = 0;
while (resultArr[index]) {
tempArr.push("" + resultArr[index] + str[i]);
index++;
}
resultArr = resultArr.concat(tempArr);
}
return resultArr;
};
console.log(combinations("dog"));
Approach 4: Using Recursion
- In this approach, we'll utilize recursion to generate all possible combinations of a string.
- We'll create a recursive function that takes the original string, a current index, and a current combination as parameters.
- At each step, the function will either include or exclude the character at the current index in the combination, generating all possible combinations.
let combinationsRecursive = (str, index = 0,
current = "") => {
if (index === str.length) {
console.log(current); // Output each combination
return;
}
// Include the character at the current index
combinationsRecursive(str, index + 1,
current + str[index]);
// Exclude the character at the current index
combinationsRecursive(str, index + 1, current);
};
console.log(combinationsRecursive("dog"));
This recursive approach effectively generates all possible combinations of the given string by including or excluding characters at each step of the recursion.