Skip to content

Commit 4434e96

Browse files
committedApr 17, 2018
Add permutations and combinations.
1 parent e8de00c commit 4434e96

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Combinations
2+
3+
When the order doesn't matter, it is a **Combination**.
4+
5+
When the order **does** matter it is a **Permutation**.
6+
7+
**"My fruit salad is a combination of apples, grapes and bananas"**
8+
We don't care what order the fruits are in, they could also be
9+
"bananas, grapes and apples" or "grapes, apples and bananas",
10+
its the same fruit salad.
11+
12+
## Combinations without repetitions
13+
14+
## Combinations with repetitions
15+
16+
## References
17+
18+
[Math Is Fun](https://www.mathsisfun.com/combinatorics/combinations-permutations.html)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import permutateWithRepetition from '../permutateWithRepetitions';
2+
3+
describe('permutateWithRepetition', () => {
4+
it('should permutate string with repetition', () => {
5+
const permutations0 = permutateWithRepetition('');
6+
expect(permutations0).toEqual([]);
7+
8+
const permutations1 = permutateWithRepetition('A');
9+
expect(permutations1).toEqual(['A']);
10+
11+
const permutations2 = permutateWithRepetition('AB');
12+
expect(permutations2).toEqual([
13+
'AA',
14+
'AB',
15+
'BA',
16+
'BB',
17+
]);
18+
19+
const permutations3 = permutateWithRepetition('ABC');
20+
expect(permutations3).toEqual([
21+
'AAA',
22+
'AAB',
23+
'AAC',
24+
'ABA',
25+
'ABB',
26+
'ABC',
27+
'ACA',
28+
'ACB',
29+
'ACC',
30+
'BAA',
31+
'BAB',
32+
'BAC',
33+
'BBA',
34+
'BBB',
35+
'BBC',
36+
'BCA',
37+
'BCB',
38+
'BCC',
39+
'CAA',
40+
'CAB',
41+
'CAC',
42+
'CBA',
43+
'CBB',
44+
'CBC',
45+
'CCA',
46+
'CCB',
47+
'CCC',
48+
]);
49+
50+
const permutations4 = permutateWithRepetition('ABCD');
51+
expect(permutations4.length).toBe(4 * 4 * 4 * 4);
52+
});
53+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @param {string} str
3+
* @return {string[]}
4+
*/
5+
export default function permutateWithRepetition(str) {
6+
// There is no permutations for empty string.
7+
if (!str || str.length === 0) {
8+
return [];
9+
}
10+
11+
// There is only one permutation for the 1-character string.
12+
if (str.length === 1) {
13+
return [str];
14+
}
15+
16+
// Let's create initial set of permutations.
17+
let previousPermutations = Array.from(str);
18+
let currentPermutations = [];
19+
let permutationSize = 1;
20+
21+
// While the size of each permutation is less then or equal to string length...
22+
while (permutationSize < str.length) {
23+
// Reset all current permutations.
24+
currentPermutations = [];
25+
26+
for (let pemIndex = 0; pemIndex < previousPermutations.length; pemIndex += 1) {
27+
for (let charIndex = 0; charIndex < str.length; charIndex += 1) {
28+
const currentPermutation = previousPermutations[pemIndex] + str[charIndex];
29+
currentPermutations.push(currentPermutation);
30+
}
31+
}
32+
33+
// Make current permutations to be the previous ones.
34+
previousPermutations = currentPermutations.slice(0);
35+
36+
// Increase permutation size counter.
37+
permutationSize += 1;
38+
}
39+
40+
return currentPermutations;
41+
}

‎src/algorithms/string/permutations/permutateWithoutRepetitions.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @param {string} str
3+
* @return {string[]}
4+
*/
15
export default function permutateWithoutRepetitions(str) {
26
if (str.length === 0) {
37
return [];

0 commit comments

Comments
 (0)