Skip to content

Instantly share code, notes, and snippets.

@jinwolf
Created December 19, 2015 08:28
Show Gist options
  • Select an option

  • Save jinwolf/4520f5aa5c882c2a0fd3 to your computer and use it in GitHub Desktop.

Select an option

Save jinwolf/4520f5aa5c882c2a0fd3 to your computer and use it in GitHub Desktop.
Prints combinations of given string
var combo = (function() {
var limit = 0;
function doCombo(charList, buffer, level, start) {
if (level === limit) {
console.log(buffer.join(' * '));
return;
}
for (var i = start; i < charList.length; i++) {
var char = charList[i];
buffer.push(char);
doCombo(charList, buffer, level + 1, i + 1);
buffer.pop();
}
}
return function(str, k) {
var charList = str.split('');
var buffer = [];
var level = 0;
var start = 0;
var used = [];
limit = k;
doCombo(charList, buffer, level, start);
}
})();
combo('abcd', 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment