Skip to content

chore: string permutation #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions String permutation/permutation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* There is a given string input and
* this script will create all permutations of this string.
*
* * With input 'aabb'
* * Thre result will be ['aabb', 'abab', 'abba', 'baab', 'baba', 'bbaa']
*
*/

let input = 'aabb';

// variable that will store all permutations
let permutations = [];
function permute(str, arr) {
if (arr.length == 0 && !permutations.includes(str)) {
permutations.push(str);
} else {
for (let i = 0; i < arr.length; i++) {
let arr2 = arr.slice();
arr2.splice(i, 1);
permute(str + arr[i], arr2);
}
}
}

// call the function for the permutation
permute('', input.split(''));
// print the result
console.log(permutations);