Skip to content

Commit eb815c8

Browse files
committed
Add string permutation algorithm.
1 parent 27407d6 commit eb815c8

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# String Permutations
2+
3+
A permutation, also called an “arrangement number” or “order”, is a rearrangement of
4+
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.
6+
7+
Below are the permutations of string `ABC`.
8+
9+
`ABC ACB BAC BCA CBA CAB`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import permutateString from '../permutateString';
2+
3+
describe('permutateString', () => {
4+
it('should permutate string', () => {
5+
const permutations0 = permutateString('');
6+
expect(permutations0).toEqual([]);
7+
8+
const permutations1 = permutateString('A');
9+
expect(permutations1).toEqual(['A']);
10+
11+
const permutations2 = permutateString('AB');
12+
expect(permutations2.length).toBe(2);
13+
expect(permutations2).toEqual([
14+
'BA',
15+
'AB',
16+
]);
17+
18+
const permutations6 = permutateString('AA');
19+
expect(permutations6.length).toBe(2);
20+
expect(permutations6).toEqual([
21+
'AA',
22+
'AA',
23+
]);
24+
25+
const permutations3 = permutateString('ABC');
26+
expect(permutations3.length).toBe(2 * 3);
27+
expect(permutations3).toEqual([
28+
'CBA',
29+
'BCA',
30+
'BAC',
31+
'CAB',
32+
'ACB',
33+
'ABC',
34+
]);
35+
36+
const permutations4 = permutateString('ABCD');
37+
expect(permutations4.length).toBe(2 * 3 * 4);
38+
expect(permutations4).toEqual([
39+
'DCBA',
40+
'CDBA',
41+
'CBDA',
42+
'CBAD',
43+
'DBCA',
44+
'BDCA',
45+
'BCDA',
46+
'BCAD',
47+
'DBAC',
48+
'BDAC',
49+
'BADC',
50+
'BACD',
51+
'DCAB',
52+
'CDAB',
53+
'CADB',
54+
'CABD',
55+
'DACB',
56+
'ADCB',
57+
'ACDB',
58+
'ACBD',
59+
'DABC',
60+
'ADBC',
61+
'ABDC',
62+
'ABCD',
63+
]);
64+
65+
const permutations5 = permutateString('ABCDEF');
66+
expect(permutations5.length).toBe(2 * 3 * 4 * 5 * 6);
67+
});
68+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export default function permutateString(str) {
2+
if (str.length === 0) {
3+
return [];
4+
}
5+
6+
if (str.length === 1) {
7+
return [str];
8+
}
9+
10+
const permutations = [];
11+
12+
// Get all permutations of string of length (n - 1).
13+
const previousString = str.substring(0, str.length - 1);
14+
const previousPermutations = permutateString(previousString);
15+
16+
// Insert last character into every possible position of every previous permutation.
17+
const lastCharacter = str.substring(str.length - 1);
18+
19+
for (
20+
let permutationIndex = 0;
21+
permutationIndex < previousPermutations.length;
22+
permutationIndex += 1
23+
) {
24+
const currentPermutation = previousPermutations[permutationIndex];
25+
26+
// Insert strLastCharacter into every possible position of currentPermutation.
27+
for (let positionIndex = 0; positionIndex <= currentPermutation.length; positionIndex += 1) {
28+
const permutationPrefix = currentPermutation.substr(0, positionIndex);
29+
const permutationSuffix = currentPermutation.substr(positionIndex);
30+
permutations.push(permutationPrefix + lastCharacter + permutationSuffix);
31+
}
32+
}
33+
34+
return permutations;
35+
}

0 commit comments

Comments
 (0)