Skip to content

Commit 3f939b7

Browse files
committed
Add two implementations of the 'is unique' algorithm
1 parent 4621ca0 commit 3f939b7

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ An algorithm is a finite sequence of well-defined, computer-implementable instru
2727
* [Binary search](src/algorithms/search/binary) - find a position of the element in a sorted one-dimensional array;
2828
* **String**
2929
* [Document distance](src/algorithms/string/document-distance) - measure similarity between two strings;
30+
* [Is unique](src/algorithms/string/unique) - check a string for uniqueness;
3031
* **Sort**
3132
* [Insertion sort](src/algorithms/sort/insertion/simple) - sort an array with a simple insertion algorithm;
3233
* [Merge sort](src/algorithms/sort/merge) - sort an array with a merge sort algorithm;
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Is Unique
2+
3+
Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?
4+
5+
## uniqueV1
6+
7+
This algorithm uses additional memory which gives you `O(n)` time and `O(n)` memory.
8+
9+
## uniqueV2
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).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { uniqueV1 } from "../uniqueV1";
2+
import { uniqueV2 } from "../uniqueV2";
3+
4+
test.each([
5+
["abc", true],
6+
["aab", false],
7+
["abcdefghijklmnopqrstuvwxyz123456789a", false],
8+
["abcdefghijklmnopqrst uvwxyz123456789ABCDEFGHJ0_!';,.", true]
9+
])("#1 the string %p has unique characters only: %p", (input, value) => {
10+
expect(uniqueV1(input)).toBe(value);
11+
});
12+
13+
test.each([
14+
["abc", true],
15+
["aab", false],
16+
["abcdefghijklmnopqrstuvwxyz123456789a", false],
17+
["abcdefghijklmnopqrst uvwxyz123456789ABCDEFGHJ0_!';,.", true]
18+
])("#2 the string %p has unique characters only: %p", (input, value) => {
19+
expect(uniqueV2(input)).toBe(value);
20+
});
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Check if the input has unique characters only
3+
* - Time complexity: O(n)
4+
* - Memory complexity: O(n)
5+
* @param {string} input
6+
* @returns boolean
7+
*/
8+
export function uniqueV1(input: string): boolean {
9+
const dict = {};
10+
for (const char of input) {
11+
if (dict[char]) {
12+
return false;
13+
}
14+
15+
dict[char] = 1;
16+
}
17+
18+
return true;
19+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { sort } from "../../sort/heap/heapSort";
2+
3+
/**
4+
* Check if the input has unique characters only
5+
* - Time complexity: O(n * log n)
6+
* @param {string} input
7+
* @returns boolean
8+
*/
9+
export function uniqueV2(input: string): boolean {
10+
const sorted = sort(input.split("").map((s) => s.charCodeAt(0)));
11+
for (let i = 0, j = sorted.length - 1; i < j; i++) {
12+
if (sorted[i] === sorted[i + 1]) {
13+
return false;
14+
}
15+
}
16+
return true;
17+
}

0 commit comments

Comments
 (0)