DAY 22
1

# 22 - Permutations

Don't say so much, just coding...

## Instruction

In this kata you have to create all permutations of an input string and remove duplicates, if present. This means, you have to shuffle all letters from the input in all possible orders.

Examples:

``````  permutations('a'); # ['a']
permutations('ab'); # ['ab', 'ba']
permutations('aabb'); # ['aabb', 'abab', 'abba', 'baab', 'baba', 'bbaa']
``````

The order of the permutations doesn't matter.

### Ruby

#### Init

``````  def permutations(string)
end
``````

#### Sample Testing

``````  #Test.assert_equals(permutations('a').sort, ['a']);
Test.assert_equals(permutations('ab').sort, ['ab', 'ba'])
Test.assert_equals(permutations('aabb').sort, ['aabb', 'abab', 'abba', 'baab', 'baba', 'bbaa'])
``````

### Javascript

#### Init

``````  function permutations(string) {
}
``````

#### Sample Testing

``````  describe('permutations', function() {
it('examples from description', function() {
Test.assertSimilar(permutations('a'), ['a']);
Test.assertSimilar(permutations('ab').sort(), ['ab', 'ba'].sort());
Test.assertSimilar(permutations('aabb').sort(), ['aabb', 'abab', 'abba', 'baab', 'baba', 'bbaa'].sort());
});
});
``````

# Solution

### Ruby

``````  # Solution 1
def permutations(string)
string.chars.permutation.map(&:join).uniq
end
``````

### Javascript

``````  // Solution 1
function permutations(string) {
if (string.length <= 1)
return [string];

var result = [];
for(var i = 0; i < string.length; i++){
result = result.concat(permutations(string.substring(0, i) + string.substring(i + 1)).map(function(e) {
return string[i] + e;
}).filter(function(e) {
return result.indexOf(e) === -1;
}));
}
return result;
}
}
``````