Last active
June 17, 2017 05:38
-
-
Save sghiassy/669cae13af2632a3ee16b0c633982343 to your computer and use it in GitHub Desktop.
A string permutation example written in JavaScript (ES6)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NOTE: This is written using ES6 - make sure your JavaScript compiler is ES6 compliant | |
class StringPermutation { | |
constructor(str) { | |
this.originalString = str.slice(); // deep copy string | |
} | |
printAllPermutationsWithDuplicates() { | |
this.permutate( | |
this.originalString.split('').fill(1), // make string an array and set values to 1: [1,1,1,1] | |
this.originalString, // "AABC" | |
new Array(this.originalString.length).fill(0) // create blank array the size of the string with default defaulting to 0: [0,0,0,0] | |
); | |
} | |
printAllPermutationsRemoveDuplicates() { | |
let map = new Map(); | |
for (let ch of this.originalString) { | |
map.set(ch, map.get(ch) === undefined ? 1 : map.get(ch) + 1); | |
} | |
this.permutate( | |
[...map.values()], // create an array from the values of the map: [2, 1, 1] | |
[...map.keys()], // create an array from the keys of the map: ['A', 'B', 'C'] | |
new Array(this.originalString.length).fill(0) // create blank array the size of the string with default defaulting to 0: [0,0,0,0] | |
); | |
} | |
permutate(setCount, keyMap, result = [], depth = 0) { | |
if (depth == result.length) { | |
console.log(result); | |
return; | |
} | |
for (let i = 0; i < setCount.length; i++) { | |
if (setCount[i] > 0) { | |
result[depth] = keyMap[i]; | |
setCount[i]--; | |
this.permutate(setCount, keyMap, result, depth + 1); | |
setCount[i]++; | |
} | |
} | |
} | |
} | |
let stringPermutation = new StringPermutation('AABC'); | |
console.log(` | |
PRINTING ALL PERMUTATIONS WITH DUPLICATES SHOWING | |
=================================================`); | |
stringPermutation.printAllPermutationsWithDuplicates(); | |
console.log(`\n | |
PRINTING ALL PERMUTATIONS WITH DUPLICATES REMOVED | |
=================================================`); | |
stringPermutation.printAllPermutationsRemoveDuplicates(); | |
/// OUTPUT | |
// | |
// PRINTING ALL PERMUTATIONS WITH DUPLICATES SHOWING | |
// ================================================= | |
// [ 'A', 'A', 'B', 'C' ] | |
// [ 'A', 'A', 'C', 'B' ] | |
// [ 'A', 'B', 'A', 'C' ] | |
// [ 'A', 'B', 'C', 'A' ] | |
// [ 'A', 'C', 'A', 'B' ] | |
// [ 'A', 'C', 'B', 'A' ] | |
// [ 'A', 'A', 'B', 'C' ] | |
// [ 'A', 'A', 'C', 'B' ] | |
// [ 'A', 'B', 'A', 'C' ] | |
// [ 'A', 'B', 'C', 'A' ] | |
// [ 'A', 'C', 'A', 'B' ] | |
// [ 'A', 'C', 'B', 'A' ] | |
// [ 'B', 'A', 'A', 'C' ] | |
// [ 'B', 'A', 'C', 'A' ] | |
// [ 'B', 'A', 'A', 'C' ] | |
// [ 'B', 'A', 'C', 'A' ] | |
// [ 'B', 'C', 'A', 'A' ] | |
// [ 'B', 'C', 'A', 'A' ] | |
// [ 'C', 'A', 'A', 'B' ] | |
// [ 'C', 'A', 'B', 'A' ] | |
// [ 'C', 'A', 'A', 'B' ] | |
// [ 'C', 'A', 'B', 'A' ] | |
// [ 'C', 'B', 'A', 'A' ] | |
// [ 'C', 'B', 'A', 'A' ] | |
// | |
// | |
// PRINTING ALL PERMUTATIONS WITH DUPLICATES REMOVED | |
// ================================================= | |
// [ 'A', 'A', 'B', 'C' ] | |
// [ 'A', 'A', 'C', 'B' ] | |
// [ 'A', 'B', 'A', 'C' ] | |
// [ 'A', 'B', 'C', 'A' ] | |
// [ 'A', 'C', 'A', 'B' ] | |
// [ 'A', 'C', 'B', 'A' ] | |
// [ 'B', 'A', 'A', 'C' ] | |
// [ 'B', 'A', 'C', 'A' ] | |
// [ 'B', 'C', 'A', 'A' ] | |
// [ 'C', 'A', 'A', 'B' ] | |
// [ 'C', 'A', 'B', 'A' ] | |
// [ 'C', 'B', 'A', 'A' ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment