-
Notifications
You must be signed in to change notification settings - Fork 26
/
lesson8.ts
66 lines (57 loc) · 1.52 KB
/
lesson8.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function run(): void {
console.log("The permutations of 'abc' are:");
printPermutations('abc', '');
console.log();
console.log("The reverse of 'abc' is: " + reverseString('abc'));
}
/**
* Prints all the permutations of a string.
*
* @param value The string to permute.
* @param answer The current permutation.
*/
function printPermutations(value: string, answer: string): void {
if (value.length === 0) {
console.log(answer);
return;
}
for (let i = 0; i < value.length; i++) {
const ch = value.charAt(i);
const left = value.substring(0, i);
const right = value.substring(i + 1);
const rest = left + right;
printPermutations(rest, answer + ch);
}
}
/**
* Reverses a string by swapping the front half of the characters with the back half.
*
* @param input The string to reverse.
* @return The reversed string.
*/
function reverseString(input: string): string {
if (input.length === 0) {
return input;
}
const charArray = input.split('');
for (let i = 0; i < charArray.length / 2; i++) {
// Compute the corresponding index from the back of the string.
const j = charArray.length - i - 1;
swapCharacters(charArray, i, j);
}
return charArray.join('');
}
/**
* Swaps the characters in the provided character array.
*
* @param charArray
* @param i
* @param j
*/
function swapCharacters(charArray: string[], i: number, j: number): void {
const temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
}
// Execute the main `run` method.
run();