How to use 'count all permutations of a string' in JavaScript

Every line of 'count all permutations of a string' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
20export default function StringPermutationsRecursive(string: string): number {
21 if (string.length === 1) {
22 permutations++;
23 }
24
25 const _string = string.split('');
26
27 for (let i = 0; i < _string.length; i++) {
28 StringPermutationsRecursive(
29 _string.filter((e: any, _i: number): boolean => _i !== i).join('')
30 );
31 }
32
33 return permutations;
34}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

100function permutate(chars) { //recursive: generates the permutations
101 if(chars.length === 0) {
102 permutation_array.push(nextWord.join(''));
103 }
104
105 for (var i=0; i < chars.length; i++){
106 chars.push(chars.shift()); //rotate the characters
107 nextWord.push(chars[0]); //use the first char in the array
108 permutate(chars.slice(1)); //Recurse: array-less-one-char
109 nextWord.pop(); //clear for nextWord (multiple pops)
110 }
111}

Related snippets