Skip to content

Commit d01b781

Browse files
committed
Add the 'check permutation' algorithms
1 parent 3f939b7 commit d01b781

File tree

6 files changed

+77
-1
lines changed

6 files changed

+77
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ An algorithm is a finite sequence of well-defined, computer-implementable instru
2828
* **String**
2929
* [Document distance](src/algorithms/string/document-distance) - measure similarity between two strings;
3030
* [Is unique](src/algorithms/string/unique) - check a string for uniqueness;
31+
* [Check permutation](src/algorithms/string/permutation) - check if a string is a permutation of the other;
3132
* **Sort**
3233
* [Insertion sort](src/algorithms/sort/insertion/simple) - sort an array with a simple insertion algorithm;
3334
* [Merge sort](src/algorithms/sort/merge) - sort an array with a merge sort algorithm;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Check Permutation
2+
3+
Given two st4rings, write a method to decide if one is a permutation of the other.
4+
5+
## permutationV1
6+
7+
This algorithm uses additional memory which gives you `O(n)` time and `O(n)` memory.
8+
9+
## permutationV2
10+
11+
This algorithm does not use additional memory but alters the input. This one gives you `O(n * log n)` time (depends on the sorting algorithm you choose).
12+
13+
1, 84, 122, 131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { permutationsV1 } from "../permutationsV1";
2+
import { permutationsV2 } from "../permutationsV2";
3+
4+
test.each([
5+
["abc", "cab", true],
6+
["aaaaaaaaaaaaaaa", "bbbbbbbb", false]
7+
])("#1 the string %p is the permutation of the string %p: %p", (a, b, value) => {
8+
expect(permutationsV1(a, b)).toBe(value);
9+
});
10+
11+
test.each([
12+
["abc", "cab", true],
13+
["aaaaaaaaaaaaaaa", "bbbbbbbb", false]
14+
])("#2 the string %p is the permutation of the string %p: %p", (a, b, value) => {
15+
expect(permutationsV2(a, b)).toBe(value);
16+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Check if the string a is a permutation of the string b
3+
* @param {string} a
4+
* @param {string} b
5+
* @returns boolean
6+
*/
7+
export function permutationsV1(a: string, b: string): boolean {
8+
if(a === b) {
9+
return true;
10+
}
11+
12+
if(a.length !== b.length) {
13+
return false;
14+
}
15+
16+
const dict = {};
17+
for(let i = 0, j = a.length; i < j; i++) {
18+
dict[a[i]] = (dict[a[i]] || 0) + 1;
19+
dict[b[i]] = (dict[b[i]] || 0) + 1;
20+
}
21+
22+
return !Object.values(dict).find((v: number) => v % 2 != 0);
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { sort } from "../../sort/heap/heapSort";
2+
3+
/**
4+
* Check if the string a is a permutation of the string b
5+
* @param {string} a
6+
* @param {string} b
7+
* @returns boolean
8+
*/
9+
export function permutationsV2(a: string, b: string): boolean {
10+
const combined = a.split("")
11+
.concat(b.split(""))
12+
.map((el) => el.charCodeAt(0));
13+
14+
const sorted = sort(combined);
15+
16+
for (let i = 0, j = combined.length; i < j; i += 2) {
17+
if (sorted[i] != sorted[i + 1]) {
18+
return false;
19+
}
20+
}
21+
22+
return true;
23+
}

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"module": "commonjs",
4-
"target": "es6",
4+
"target": "ESNext",
55
"noImplicitAny": false,
66
"sourceMap": false,
77
"rootDir": "src",

0 commit comments

Comments
 (0)