Skip to content

Commit 21999cc

Browse files
committed
Add permutations and combinations.
1 parent ccaddf8 commit 21999cc

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* [Least Common Multiple (LCM)](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/least-common-multiple)
3737
* [Fisher–Yates Shuffle](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fisher-yates) - random permutation of a finite sequence
3838
* **String**
39-
* [String Permutations](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/permutations)
39+
* [String Permutations](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/permutations) - with and without repetitions
4040
* Combination
4141
* Minimum Edit distance (Levenshtein Distance)
4242
* Hamming

src/algorithms/string/permutations/README.md

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
1-
# String Permutations
1+
# Permutations
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+
**"The combination to the safe is 472"**. We do care about the order. `724` won't work, nor will `247`.
8+
It has to be exactly `4-7-2`.
9+
10+
## Permutations without repetitions
211

312
A permutation, also called an “arrangement number” or “order”, is a rearrangement of
413
the elements of an ordered list `S` into a one-to-one correspondence with `S` itself.
5-
A string of length `n` has `n!` permutation.
614

715
Below are the permutations of string `ABC`.
816

917
`ABC ACB BAC BCA CBA CAB`
1018

11-
When the order doesn't matter, it is a **Combination**.
19+
Or for example the first three people in a running race: you can't be first and second.
1220

13-
When the order **does** matter it is a **Permutation**.
21+
**Number of combinations**
22+
23+
```
24+
n * (n-1) * (n -2) * ... * 1 = n!
25+
```
26+
27+
## Permutations with repetitions
28+
29+
When repetition is allowed we have permutations with repetitions.
30+
For example the the lock below: it could be `333`.
1431

1532
![Permutation Lock](https://www.mathsisfun.com/combinatorics/images/combination-lock.jpg)
1633

17-
Permutation with repetitions example
34+
**Number of combinations**
1835

19-
![Permutation With Repetitions](https://upload.wikimedia.org/wikipedia/commons/6/69/Permutations-With-Repetition.gif)
36+
```
37+
n * n * n ... (r times) = n^r
38+
```
2039

2140
## References
2241

src/algorithms/string/permutations/__test__/permutateString.test.js renamed to src/algorithms/string/permutations/__test__/permutateWithoutRepetitions.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
import permutateString from '../permutateString';
1+
import permutateWithoutRepetitions from '../permutateWithoutRepetitions';
22

33
describe('permutateString', () => {
44
it('should permutate string', () => {
5-
const permutations0 = permutateString('');
5+
const permutations0 = permutateWithoutRepetitions('');
66
expect(permutations0).toEqual([]);
77

8-
const permutations1 = permutateString('A');
8+
const permutations1 = permutateWithoutRepetitions('A');
99
expect(permutations1).toEqual(['A']);
1010

11-
const permutations2 = permutateString('AB');
11+
const permutations2 = permutateWithoutRepetitions('AB');
1212
expect(permutations2.length).toBe(2);
1313
expect(permutations2).toEqual([
1414
'BA',
1515
'AB',
1616
]);
1717

18-
const permutations6 = permutateString('AA');
18+
const permutations6 = permutateWithoutRepetitions('AA');
1919
expect(permutations6.length).toBe(2);
2020
expect(permutations6).toEqual([
2121
'AA',
2222
'AA',
2323
]);
2424

25-
const permutations3 = permutateString('ABC');
25+
const permutations3 = permutateWithoutRepetitions('ABC');
2626
expect(permutations3.length).toBe(2 * 3);
2727
expect(permutations3).toEqual([
2828
'CBA',
@@ -33,7 +33,7 @@ describe('permutateString', () => {
3333
'ABC',
3434
]);
3535

36-
const permutations4 = permutateString('ABCD');
36+
const permutations4 = permutateWithoutRepetitions('ABCD');
3737
expect(permutations4.length).toBe(2 * 3 * 4);
3838
expect(permutations4).toEqual([
3939
'DCBA',
@@ -62,7 +62,7 @@ describe('permutateString', () => {
6262
'ABCD',
6363
]);
6464

65-
const permutations5 = permutateString('ABCDEF');
65+
const permutations5 = permutateWithoutRepetitions('ABCDEF');
6666
expect(permutations5.length).toBe(2 * 3 * 4 * 5 * 6);
6767
});
6868
});

src/algorithms/string/permutations/permutateString.js renamed to src/algorithms/string/permutations/permutateWithoutRepetitions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function permutateString(str) {
1+
export default function permutateWithoutRepetitions(str) {
22
if (str.length === 0) {
33
return [];
44
}
@@ -11,7 +11,7 @@ export default function permutateString(str) {
1111

1212
// Get all permutations of string of length (n - 1).
1313
const previousString = str.substring(0, str.length - 1);
14-
const previousPermutations = permutateString(previousString);
14+
const previousPermutations = permutateWithoutRepetitions(previousString);
1515

1616
// Insert last character into every possible position of every previous permutation.
1717
const lastCharacter = str.substring(str.length - 1);

0 commit comments

Comments
 (0)