M
mdr
Hi,
I have a string permutation function that generates all permutations, of all lengths, for a given string input. I don't like that I have to create a dictionary object to check if that combination has already been created. I think the whole function could be optimized, honestly.
<script type="text/javascript">
var list = [];
function permutate(permutation, addendum) {
if (addendum.length == 0) {
if (list[permutation] == undefined) {
list[permutation] = 1;
console.log(permutation);
}
return;
}
for (var i = 0; i < addendum.length; i++) {
permutate(permutation + addendum.charAt(i), addendum.substring(0, i) + addendum.substring(i+1, addendum.length));
}
for (var i = permutation.length; i > 0; i--) {
permutate("", permutation.substring(0, i));
}
}
permutate("", "abc");
</script>
Does anyone know how to do this more efficiently?
I have a string permutation function that generates all permutations, of all lengths, for a given string input. I don't like that I have to create a dictionary object to check if that combination has already been created. I think the whole function could be optimized, honestly.
<script type="text/javascript">
var list = [];
function permutate(permutation, addendum) {
if (addendum.length == 0) {
if (list[permutation] == undefined) {
list[permutation] = 1;
console.log(permutation);
}
return;
}
for (var i = 0; i < addendum.length; i++) {
permutate(permutation + addendum.charAt(i), addendum.substring(0, i) + addendum.substring(i+1, addendum.length));
}
for (var i = permutation.length; i > 0; i--) {
permutate("", permutation.substring(0, i));
}
}
permutate("", "abc");
</script>
Does anyone know how to do this more efficiently?