R
E
A
C
T
O
xamples
epeat
ode
pproach
ptimize
est
{String Permutations}
The Question
Given a string, return an array of all the permutations of that string. The permutations of the string should be the same length as the original string (i.e. use each letter in the string exactly once) but do not need to be actual words.
stringPermutations('one') //should return [ 'eon', 'eno' 'neo', 'noe', 'one', 'oen']
stringPermutations('app') //should return [ 'app','pap','ppa']
stringPermutations('nn') //should return [ 'nn' ]
Notes
- The array that is returned should only contain unique values
- The array that is returned should be sorted alphabetically
Approach
A string of one character had one permutation
'a' -> [ 'a']
After that, for each additional letter, add it to each possible space in the current results
stringPermutations('abc')
insert 'a'
[ 'a' ]
Approach
A string of one character had one permutation
'a' -> [ 'a']
After that, for each additional letter, add it to each possible space in the current results
stringPermutations('abc')
[ 'a' ]
[ 'a' ]
'ab' ]
insert 'b'
[ 'ba
Approach
A string of one character had one permutation
'a' -> [ 'a']
After that, for each additional letter, add it to each possible space in the current results
stringPermutations('abc')
[ 'a' ]
[ 'ba' , 'ab' ]
insert 'c'
['cba'
'bca'
'bac'
'cab'
'acb'
'abc' ]
Possible Solution
function stringPermutations(str) {
var results = [ ];
var letters = str.split('');
results.push([letters.shift()]) //add first letter (as an array) to results
while (letters.length) {
var curLetter = letters.shift();
var tmpResults = [ ];
results.forEach(function(curResult) {
for (var i = 0; i<= curResult.length; i++) {
var tmp = curResult.slice(); //make copy so we can modify it
//insert the letter at the current position
tmp.splice(i,0,curLetter)
tmpResults.push(tmp);
}
});
results = tmpResults; //overwrite the previous results
}
results = results.map(function(letterArr) {
return letterArr.join(''); //make string from letter array
});
return results.filter(function(el,index) {
return results.indexOf(el) === index; //filter out non-unique words
}).sort();
}
Possible Recursive Solution
function recursiveStringPermutations(str) {
var results = [ ];
getPerms(str, [ ])
function getPerms(str, arr) {
if (typeof str === 'string')
//on first call, split the string into an array
str = str.split('');
if (!str.length)
//base case- push the compiled results into the results variable
results.push(arr.join(''));
for (var i = 0; i < str.length; i++) {
var letter = str.splice(i, 1);
arr.push(letter);
getPerms(str, arr); //recursive call
arr.pop();
str.splice(i, 0, letter);
}
}
return results.filter(function(el,index) {
return results.indexOf(el) === index; //filter out non-unique words
}).sort();
}
Conclusion
- Know your string and array methods.
- Think about how to break up a problem into smaller pieces.
String Permutations
By Seema Ullal
String Permutations
Technical interview problem on generating string permutations
- 4,231