Write a function to generate all combinations of a string with a given length.
HardProblem Statement
Write a function to generate all combinations of a string with a given length.
Sample Input
str = "abc", length = 2
Sample Output
[ 'ab', 'ac', 'bc' ]
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
// Generate all combinations of a string with a specific length
function getCombinations(str, length, start = 0, current = '', combinations = [ ]) {
// Check if current combination has the required length
// Loop through characters, recursively forming combinations
}
console.log(getCombinations("abc", 2));
Input
str = "abc", length = 2
Expected Output
[ 'ab', 'ac', 'bc' ]