typescript-ds-lib 0.4.6 → 0.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
- # TypeScript Data Structure Library
1
+ # TypeScript Data Structure & Algorithm Library
2
2
  [![NPM](https://img.shields.io/npm/v/typescript-ds-lib?label=npm%20package&color=limegreen)](https://www.npmjs.com/package/typescript-ds-lib)
3
3
 
4
- TypeScript data structure and algorithm implementations without external dependencies.
4
+ TypeScript data structure and algorithm implementations with no external dependencies.
5
5
 
6
6
  **Why to use this library?**
7
7
  - Fully Tested
@@ -57,6 +57,7 @@ See the [documentation](https://github.com/baloian/typescript-ds-lib/blob/master
57
57
 
58
58
  ## Algorithms
59
59
  - Binary Search (with custom comparator)
60
+ - Levenshtein Distance (calculates the edit distance between two strings)
60
61
  - Shuffle (shuffles the array in-place)
61
62
 
62
63
  ## `Map-Set` vs Native JavaScript `Map-Set`
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Calculates the Levenshtein distance (edit distance) between two strings.
3
+ *
4
+ * The Levenshtein distance is the minimum number of single-character edits
5
+ * (insertions, deletions, or substitutions) required to transform one string into another.
6
+ *
7
+ * This implementation uses space-optimized dynamic programming with O(min(m, n)) space complexity
8
+ * instead of the naive O(m * n) approach, where m and n are the lengths of the input strings.
9
+ *
10
+ * Time complexity: O(m * n)
11
+ * Space complexity: O(min(m, n))
12
+ *
13
+ * @param str1 - The first string to compare.
14
+ * @param str2 - The second string to compare.
15
+ * @returns The Levenshtein distance between the two strings (a non-negative integer).
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * levenshteinDistance('kitten', 'sitting'); // Returns 3
20
+ * levenshteinDistance('hello', 'hello'); // Returns 0
21
+ * levenshteinDistance('', 'abc'); // Returns 3
22
+ * levenshteinDistance('abc', ''); // Returns 3
23
+ * ```
24
+ */
25
+ export declare function levenshteinDistance(str1: string, str2: string): number;
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.levenshteinDistance = levenshteinDistance;
4
+ /**
5
+ * Calculates the Levenshtein distance (edit distance) between two strings.
6
+ *
7
+ * The Levenshtein distance is the minimum number of single-character edits
8
+ * (insertions, deletions, or substitutions) required to transform one string into another.
9
+ *
10
+ * This implementation uses space-optimized dynamic programming with O(min(m, n)) space complexity
11
+ * instead of the naive O(m * n) approach, where m and n are the lengths of the input strings.
12
+ *
13
+ * Time complexity: O(m * n)
14
+ * Space complexity: O(min(m, n))
15
+ *
16
+ * @param str1 - The first string to compare.
17
+ * @param str2 - The second string to compare.
18
+ * @returns The Levenshtein distance between the two strings (a non-negative integer).
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * levenshteinDistance('kitten', 'sitting'); // Returns 3
23
+ * levenshteinDistance('hello', 'hello'); // Returns 0
24
+ * levenshteinDistance('', 'abc'); // Returns 3
25
+ * levenshteinDistance('abc', ''); // Returns 3
26
+ * ```
27
+ */
28
+ function levenshteinDistance(str1, str2) {
29
+ // Early termination: if strings are identical, distance is 0
30
+ if (str1 === str2) {
31
+ return 0;
32
+ }
33
+ // Early termination: if one string is empty, distance is the length of the other
34
+ if (str1.length === 0) {
35
+ return str2.length;
36
+ }
37
+ if (str2.length === 0) {
38
+ return str1.length;
39
+ }
40
+ // Use the shorter string for the inner dimension to minimize space
41
+ const [longer, shorter] = str1.length >= str2.length ? [str1, str2] : [str2, str1];
42
+ const m = longer.length;
43
+ const n = shorter.length;
44
+ // Initialize two rows: previous and current
45
+ // prevRow represents the distances for the previous character of the longer string
46
+ let prevRow = Array.from({ length: n + 1 }, (_, i) => i);
47
+ let currRow = new Array(n + 1);
48
+ // Fill the DP table row by row
49
+ for (let i = 1; i <= m; i++) {
50
+ currRow[0] = i; // Cost of deleting all characters up to i in longer string
51
+ for (let j = 1; j <= n; j++) {
52
+ // If characters match, no operation needed (take diagonal value)
53
+ if (longer[i - 1] === shorter[j - 1]) {
54
+ currRow[j] = prevRow[j - 1];
55
+ }
56
+ else {
57
+ // Take minimum of three operations:
58
+ // 1. Deletion: prevRow[j] + 1
59
+ // 2. Insertion: currRow[j - 1] + 1
60
+ // 3. Substitution: prevRow[j - 1] + 1
61
+ currRow[j] = Math.min(prevRow[j] + 1, // deletion
62
+ currRow[j - 1] + 1, // insertion
63
+ prevRow[j - 1] + 1 // substitution
64
+ );
65
+ }
66
+ }
67
+ // Swap rows for next iteration
68
+ [prevRow, currRow] = [currRow, prevRow];
69
+ }
70
+ // The result is in prevRow[n] (since we swapped at the end)
71
+ return prevRow[n];
72
+ }
73
+ //# sourceMappingURL=levenshtein-distance.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"levenshtein-distance.js","sourceRoot":"","sources":["../../algorithms/levenshtein-distance.ts"],"names":[],"mappings":";;AAwBA,kDA4CC;AApED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,SAAgB,mBAAmB,CAAC,IAAY,EAAE,IAAY;IAC5D,6DAA6D;IAC7D,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;QAClB,OAAO,CAAC,CAAC;IACX,CAAC;IACD,iFAAiF;IACjF,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACtB,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IACD,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACtB,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IACD,mEAAmE;IACnE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IACnF,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC;IACxB,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC;IACzB,4CA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,CAAC;AACpB,CAAC"}
package/dist/index.d.ts CHANGED
@@ -14,4 +14,5 @@ export { RedBlackTree } from './lib/red-black-tree';
14
14
  export { Set } from './lib/set';
15
15
  export { Stack } from './lib/stack';
16
16
  export { binarySearch } from './algorithms/binary-search';
17
+ export { levenshteinDistance } from './algorithms/levenshtein-distance';
17
18
  export { shuffle } from './algorithms/shuffle';
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.shuffle = exports.binarySearch = exports.Stack = exports.Set = exports.RedBlackTree = exports.Queue = exports.PriorityQueue = exports.Matrix = exports.Map = exports.LinkedList = exports.Heap = exports.HashTable = exports.LinkedListGraph = exports.MatrixGraph = exports.Deque = exports.BinarySearchTree = void 0;
17
+ exports.shuffle = exports.levenshteinDistance = exports.binarySearch = exports.Stack = exports.Set = exports.RedBlackTree = exports.Queue = exports.PriorityQueue = exports.Matrix = exports.Map = exports.LinkedList = exports.Heap = exports.HashTable = exports.LinkedListGraph = exports.MatrixGraph = exports.Deque = exports.BinarySearchTree = void 0;
18
18
  __exportStar(require("./types"), exports);
19
19
  var binary_search_tree_1 = require("./lib/binary-search-tree");
20
20
  Object.defineProperty(exports, "BinarySearchTree", { enumerable: true, get: function () { return binary_search_tree_1.BinarySearchTree; } });
@@ -46,6 +46,8 @@ var stack_1 = require("./lib/stack");
46
46
  Object.defineProperty(exports, "Stack", { enumerable: true, get: function () { return stack_1.Stack; } });
47
47
  var binary_search_1 = require("./algorithms/binary-search");
48
48
  Object.defineProperty(exports, "binarySearch", { enumerable: true, get: function () { return binary_search_1.binarySearch; } });
49
+ var levenshtein_distance_1 = require("./algorithms/levenshtein-distance");
50
+ Object.defineProperty(exports, "levenshteinDistance", { enumerable: true, get: function () { return levenshtein_distance_1.levenshteinDistance; } });
49
51
  var shuffle_1 = require("./algorithms/shuffle");
50
52
  Object.defineProperty(exports, "shuffle", { enumerable: true, get: function () { return shuffle_1.shuffle; } });
51
53
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,+DAA4D;AAAnD,sHAAA,gBAAgB,OAAA;AACzB,qCAAoC;AAA3B,8FAAA,KAAK,OAAA;AACd,yDAAuD;AAA9C,2GAAA,WAAW,OAAA;AACpB,mEAAgE;AAAvD,oHAAA,eAAe,OAAA;AACxB,+CAA6C;AAApC,uGAAA,SAAS,OAAA;AAClB,mCAAkC;AAAzB,4FAAA,IAAI,OAAA;AACb,iDAA+C;AAAtC,yGAAA,UAAU,OAAA;AACnB,iCAAgC;AAAvB,0FAAA,GAAG,OAAA;AACZ,uCAAsC;AAA7B,gGAAA,MAAM,OAAA;AACf,uDAAqD;AAA5C,+GAAA,aAAa,OAAA;AACtB,qCAAoC;AAA3B,8FAAA,KAAK,OAAA;AACd,uDAAoD;AAA3C,8GAAA,YAAY,OAAA;AACrB,iCAAgC;AAAvB,0FAAA,GAAG,OAAA;AACZ,qCAAoC;AAA3B,8FAAA,KAAK,OAAA;AACd,4DAA0D;AAAjD,6GAAA,YAAY,OAAA;AACrB,gDAA+C;AAAtC,kGAAA,OAAO,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,+DAA4D;AAAnD,sHAAA,gBAAgB,OAAA;AACzB,qCAAoC;AAA3B,8FAAA,KAAK,OAAA;AACd,yDAAuD;AAA9C,2GAAA,WAAW,OAAA;AACpB,mEAAgE;AAAvD,oHAAA,eAAe,OAAA;AACxB,+CAA6C;AAApC,uGAAA,SAAS,OAAA;AAClB,mCAAkC;AAAzB,4FAAA,IAAI,OAAA;AACb,iDAA+C;AAAtC,yGAAA,UAAU,OAAA;AACnB,iCAAgC;AAAvB,0FAAA,GAAG,OAAA;AACZ,uCAAsC;AAA7B,gGAAA,MAAM,OAAA;AACf,uDAAqD;AAA5C,+GAAA,aAAa,OAAA;AACtB,qCAAoC;AAA3B,8FAAA,KAAK,OAAA;AACd,uDAAoD;AAA3C,8GAAA,YAAY,OAAA;AACrB,iCAAgC;AAAvB,0FAAA,GAAG,OAAA;AACZ,qCAAoC;AAA3B,8FAAA,KAAK,OAAA;AACd,4DAA0D;AAAjD,6GAAA,YAAY,OAAA;AACrB,0EAAwE;AAA/D,2HAAA,mBAAmB,OAAA;AAC5B,gDAA+C;AAAtC,kGAAA,OAAO,OAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-ds-lib",
3
- "version": "0.4.6",
3
+ "version": "0.4.7",
4
4
  "description": "A collection of TypeScript data structure implementations",
5
5
  "author": "Artiom Baloian <artiom.baloian@gmail.com>",
6
6
  "license": "MIT",