Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to build a string with no repeating character n separate list of characters? in JavaScript
When working with multiple arrays of characters, we often need to create combinations where each string contains exactly one character from each array with no duplicate characters. This is useful for generating unique combinations while avoiding repeated letters that might exist across different arrays.
Problem Overview
Given multiple arrays of characters, we need to build all possible strings that:
-
Contains exactly one letter from each array
-
Must not contain any repeating character (as the arrays might contain common elements)
For example, consider these three arrays with some overlapping characters:
const arr1 = ['a', 'b', 'c', 'd']; const arr2 = ['e', 'f', 'g', 'a']; const arr3 = ['m', 'n', 'o', 'g', 'k'];
Solution Implementation
The solution uses a combination of array reduction and filtering to generate all valid combinations:
const arr1 = ['a', 'b', 'c', 'd'];
const arr2 = ['e', 'f', 'g', 'a'];
const arr3 = ['m', 'n', 'o', 'g', 'k'];
const allCombinations = (...arrs) => {
let res = [];
// Generate all possible combinations using reduce
const reduced = arrs.reduce((acc, b) => acc.reduce((r, v) => {
return r.concat(b.map(el => {
return [].concat(v, el);
}))
}, [])
);
// Filter out combinations with duplicate characters
res = reduced.filter(el => new Set(el).size === el.length);
// Convert arrays to space-separated strings
return res.map(el => el.join(' '));
};
console.log(allCombinations(arr1, arr2, arr3));
[ 'a e m', 'a e n', 'a e o', 'a e k', 'a f m', 'a f n', 'a f o', 'a f g', 'a f k', 'b e m', 'b e n', 'b e o', 'b e g', 'b e k', 'b f m', 'b f n', 'b f o', 'b f g', 'b f k', 'b g m', 'b g n', 'b g o', 'b g k', 'b a m', 'b a n', 'b a o', 'b a g', 'b a k', 'c e m', 'c e n', 'c e o', 'c e g', 'c e k', 'c f m', 'c f n', 'c f o', 'c f g', 'c f k', 'c g m', 'c g n', 'c g o', 'c g k', 'c a m', 'c a n', 'c a o', 'c a g', 'c a k', 'd e m', 'd e n', 'd e o', 'd e g', 'd e k', 'd f m', 'd f n', 'd f o', 'd f g', 'd f k', 'd g m', 'd g n', 'd g o', 'd g k', 'd a m', 'd a n', 'd a o', 'd a g', 'd a k' ]
How It Works
The algorithm works in three main steps:
-
Generate all combinations: Uses nested reduce to create cartesian product of all arrays
-
Filter duplicates: Uses Set to check if array length equals unique elements count
-
Format output: Joins character arrays into space-separated strings
Alternative Approach with Recursion
const generateCombinations = (arrays) => {
const result = [];
const backtrack = (currentCombination, arrayIndex, usedChars) => {
// Base case: processed all arrays
if (arrayIndex === arrays.length) {
result.push(currentCombination.join(' '));
return;
}
// Try each character in current array
for (let char of arrays[arrayIndex]) {
if (!usedChars.has(char)) {
currentCombination.push(char);
usedChars.add(char);
backtrack(currentCombination, arrayIndex + 1, usedChars);
currentCombination.pop();
usedChars.delete(char);
}
}
};
backtrack([], 0, new Set());
return result;
};
const arrays = [['a', 'b'], ['a', 'c'], ['b', 'd']];
console.log(generateCombinations(arrays));
[ 'a c d', 'b a d', 'b c d' ]
Conclusion
Both approaches effectively generate unique character combinations from multiple arrays. The reduce method is more concise, while the recursive approach offers better control and understanding of the backtracking process.
