Skip to content

Commit b9f196b

Browse files
committed
Typescript run - needs to fix for hyperfine
1 parent 2d3f031 commit b9f196b

File tree

4 files changed

+138
-1
lines changed

4 files changed

+138
-1
lines changed

.gitignore

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
12
*.class
23
code
34
.idea/
45
*.user
56
*.o
67
*.ali
78
.aider*
8-
.env
9+
.env
10+
node_modules/
11+
target/
12+
build/
13+
out/
14+
dist/
15+
*.log
16+
*.aux
17+
*.bbl
18+
*.blg

levenshtein/typescript/code.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Calculates the Levenshtein distance between two strings using Wagner-Fischer algorithm
3+
* Space Complexity: O(min(m,n)) - only uses two arrays instead of full matrix
4+
* Time Complexity: O(m*n) where m and n are the lengths of the input strings
5+
*
6+
* npm install
7+
* npm run build
8+
* npm start -- "string1" "string2" "string3"
9+
*
10+
*/
11+
function levenshteinDistance(s1, s2) {
12+
var _a;
13+
// Early termination checks
14+
if (s1 === s2)
15+
return 0;
16+
if (s1.length === 0)
17+
return s2.length;
18+
if (s2.length === 0)
19+
return s1.length;
20+
// Make s1 the shorter string for space optimization
21+
if (s1.length > s2.length) {
22+
_a = [s2, s1], s1 = _a[0], s2 = _a[1];
23+
}
24+
var m = s1.length;
25+
var n = s2.length;
26+
// Use typed arrays for better performance
27+
var prevRow = new Uint32Array(m + 1);
28+
var currRow = new Uint32Array(m + 1);
29+
// Initialize first row
30+
for (var i = 0; i <= m; i++) {
31+
prevRow[i] = i;
32+
}
33+
// Main computation loop
34+
for (var j = 1; j <= n; j++) {
35+
currRow[0] = j;
36+
for (var i = 1; i <= m; i++) {
37+
var cost = s1[i - 1] === s2[j - 1] ? 0 : 1;
38+
// Calculate minimum of three operations
39+
var deletion = prevRow[i] + 1;
40+
var insertion = currRow[i - 1] + 1;
41+
var substitution = prevRow[i - 1] + cost;
42+
currRow[i] = Math.min(deletion, Math.min(insertion, substitution));
43+
}
44+
// Swap rows using typed array copy
45+
prevRow.set(currRow);
46+
}
47+
return prevRow[m];
48+
}
49+
// Main program
50+
function main() {
51+
var args = process.argv.slice(2);
52+
if (args.length < 2) {
53+
console.log("Please provide at least two strings as arguments.");
54+
process.exit(1);
55+
}
56+
var minDistance = -1;
57+
var times = 0;
58+
// Compare all pairs of strings
59+
for (var i = 0; i < args.length; i++) {
60+
for (var j = 0; j < args.length; j++) {
61+
if (i !== j) {
62+
var distance = levenshteinDistance(args[i], args[j]);
63+
if (minDistance === -1 || distance < minDistance) {
64+
minDistance = distance;
65+
}
66+
times++;
67+
}
68+
}
69+
}
70+
console.log("times: ".concat(times));
71+
console.log("min_distance: ".concat(minDistance));
72+
}
73+
// Run the program
74+
main();

levenshtein/typescript/package-lock.json

+47
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)