coding-agent-benchmarks 0.5.0 → 0.5.2
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 +7 -3
- package/dist/evaluator.d.ts +1 -0
- package/dist/evaluator.d.ts.map +1 -1
- package/dist/evaluator.js +1 -1
- package/dist/evaluator.js.map +1 -1
- package/dist/lcs.d.ts +33 -0
- package/dist/lcs.d.ts.map +1 -0
- package/dist/lcs.js +147 -0
- package/dist/lcs.js.map +1 -0
- package/dist/longest-common-subsequence.d.ts +45 -0
- package/dist/longest-common-subsequence.d.ts.map +1 -0
- package/dist/longest-common-subsequence.js +162 -0
- package/dist/longest-common-subsequence.js.map +1 -0
- package/dist/longestCommonSubsequence.d.ts +39 -0
- package/dist/longestCommonSubsequence.d.ts.map +1 -0
- package/dist/longestCommonSubsequence.js +166 -0
- package/dist/longestCommonSubsequence.js.map +1 -0
- package/dist/runner.js +8 -4
- package/dist/runner.js.map +1 -1
- package/dist/types.d.ts +5 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/user-registration/example.d.ts +2 -0
- package/dist/user-registration/example.d.ts.map +1 -0
- package/dist/user-registration/example.js +41 -0
- package/dist/user-registration/example.js.map +1 -0
- package/dist/user-registration/index.d.ts +9 -0
- package/dist/user-registration/index.d.ts.map +1 -0
- package/dist/user-registration/index.js +22 -0
- package/dist/user-registration/index.js.map +1 -0
- package/dist/user-registration/services/email-service.d.ts +13 -0
- package/dist/user-registration/services/email-service.d.ts.map +1 -0
- package/dist/user-registration/services/email-service.js +34 -0
- package/dist/user-registration/services/email-service.js.map +1 -0
- package/dist/user-registration/services/password-service.d.ts +5 -0
- package/dist/user-registration/services/password-service.d.ts.map +1 -0
- package/dist/user-registration/services/password-service.js +23 -0
- package/dist/user-registration/services/password-service.js.map +1 -0
- package/dist/user-registration/services/registration-service.d.ts +19 -0
- package/dist/user-registration/services/registration-service.d.ts.map +1 -0
- package/dist/user-registration/services/registration-service.js +60 -0
- package/dist/user-registration/services/registration-service.js.map +1 -0
- package/dist/user-registration/services/user-repository.d.ts +12 -0
- package/dist/user-registration/services/user-repository.d.ts.map +1 -0
- package/dist/user-registration/services/user-repository.js +24 -0
- package/dist/user-registration/services/user-repository.js.map +1 -0
- package/dist/user-registration/types.d.ts +26 -0
- package/dist/user-registration/types.d.ts.map +1 -0
- package/dist/user-registration/types.js +18 -0
- package/dist/user-registration/types.js.map +1 -0
- package/dist/user-registration/validators/email-validator.d.ts +3 -0
- package/dist/user-registration/validators/email-validator.d.ts.map +1 -0
- package/dist/user-registration/validators/email-validator.js +22 -0
- package/dist/user-registration/validators/email-validator.js.map +1 -0
- package/dist/user-registration/validators/password-validator.d.ts +3 -0
- package/dist/user-registration/validators/password-validator.d.ts.map +1 -0
- package/dist/user-registration/validators/password-validator.js +36 -0
- package/dist/user-registration/validators/password-validator.js.map +1 -0
- package/dist/user-registration/validators/required-fields-validator.d.ts +3 -0
- package/dist/user-registration/validators/required-fields-validator.d.ts.map +1 -0
- package/dist/user-registration/validators/required-fields-validator.js +17 -0
- package/dist/user-registration/validators/required-fields-validator.js.map +1 -0
- package/dist/utils/dataFetcher.d.ts +21 -0
- package/dist/utils/dataFetcher.d.ts.map +1 -0
- package/dist/utils/dataFetcher.js +71 -0
- package/dist/utils/dataFetcher.js.map +1 -0
- package/dist/utils/imageProcessor.d.ts +16 -0
- package/dist/utils/imageProcessor.d.ts.map +1 -0
- package/dist/utils/imageProcessor.js +121 -0
- package/dist/utils/imageProcessor.js.map +1 -0
- package/dist/utils/lcs.d.ts +42 -0
- package/dist/utils/lcs.d.ts.map +1 -0
- package/dist/utils/lcs.js +135 -0
- package/dist/utils/lcs.js.map +1 -0
- package/dist/utils/notifications.d.ts +2 -0
- package/dist/utils/notifications.d.ts.map +1 -0
- package/dist/utils/notifications.js +8 -0
- package/dist/utils/notifications.js.map +1 -0
- package/dist/utils/timeUtils.d.ts +16 -1
- package/dist/utils/timeUtils.d.ts.map +1 -1
- package/dist/utils/timeUtils.js +54 -17
- package/dist/utils/timeUtils.js.map +1 -1
- package/dist/utils/userReport.d.ts +10 -0
- package/dist/utils/userReport.d.ts.map +1 -0
- package/dist/utils/userReport.js +19 -0
- package/dist/utils/userReport.js.map +1 -0
- package/dist/validators/llmJudge.d.ts.map +1 -1
- package/dist/validators/llmJudge.js +10 -6
- package/dist/validators/llmJudge.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -140,7 +140,7 @@ module.exports = {
|
|
|
140
140
|
// Default adapter to use
|
|
141
141
|
defaultAdapter: 'copilot',
|
|
142
142
|
|
|
143
|
-
// Default LLM model for judge
|
|
143
|
+
// Default LLM model for judge validation
|
|
144
144
|
defaultModel: 'openai/gpt-5',
|
|
145
145
|
|
|
146
146
|
// Default timeout for code generation (milliseconds)
|
|
@@ -390,7 +390,8 @@ Run benchmark evaluations.
|
|
|
390
390
|
| `--category <categories>` | Filter by category (comma-separated) | `typescript,react` |
|
|
391
391
|
| `--tag <tags>` | Filter by tags (comma-separated) | `safety,types` |
|
|
392
392
|
| `--adapter <type>` | Adapter to use | `copilot` or `claude-code` |
|
|
393
|
-
| `--model <model>` |
|
|
393
|
+
| `--model <model>` | Model for the coding agent adapter | - |
|
|
394
|
+
| `--judge-model <model>` | LLM model for judge validation | `openai/gpt-5` |
|
|
394
395
|
| `--threshold <number>` | Minimum passing score | `0.8` |
|
|
395
396
|
| `--verbose` | Show detailed output | - |
|
|
396
397
|
| `--output <file>` | Export JSON report | `report.json` |
|
|
@@ -416,6 +417,7 @@ Test LLM judge with a custom prompt (for debugging).
|
|
|
416
417
|
| Option | Description |
|
|
417
418
|
|--------|-------------|
|
|
418
419
|
| `--model <model>` | LLM model to use |
|
|
420
|
+
| `--judge-model <model>` | Alias for --model |
|
|
419
421
|
|
|
420
422
|
## Understanding Output
|
|
421
423
|
|
|
@@ -466,7 +468,7 @@ async function runEvaluation() {
|
|
|
466
468
|
|
|
467
469
|
const evaluator = new Evaluator({
|
|
468
470
|
adapter: 'copilot',
|
|
469
|
-
|
|
471
|
+
judgeModel: 'openai/gpt-5',
|
|
470
472
|
verbose: true,
|
|
471
473
|
saveBaseline: config.saveBaseline,
|
|
472
474
|
compareBaseline: config.compareBaseline,
|
|
@@ -540,6 +542,8 @@ gh auth login
|
|
|
540
542
|
|
|
541
543
|
Run `npx coding-agent-benchmarks check` to verify authentication status.
|
|
542
544
|
|
|
545
|
+
> **Note:** GitHub Models API is rate limited on the free tier (e.g., 15 req/min, 150 req/day for low-tier models). If you hit rate limits running many scenarios, consider [opting in to paid usage](https://docs.github.com/en/billing/managing-billing-for-your-products/about-billing-for-github-models) for higher limits.
|
|
546
|
+
|
|
543
547
|
## Requirements
|
|
544
548
|
|
|
545
549
|
- Node.js >= 18.0.0
|
package/dist/evaluator.d.ts
CHANGED
package/dist/evaluator.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"evaluator.d.ts","sourceRoot":"","sources":["../src/evaluator.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,WAAW,EAEX,YAAY,EACZ,gBAAgB,EAChB,gBAAgB,EAEjB,MAAM,SAAS,CAAC;AAQjB,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAG9D;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,kBAAkB,EAAE,CAAC,SAAS,EAAE,YAAY,EAAE,KAAK,IAAI,CAAC;IACxD,gBAAgB,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,KAAK,IAAI,CAAC;IACvE,qBAAqB,EAAE,CAAC,UAAU,EAAE,MAAM,KAAK,IAAI,CAAC;IACpD,qBAAqB,EAAE,CAAC,UAAU,EAAE,MAAM,KAAK,IAAI,CAAC;IACpD,mBAAmB,EAAE,CACnB,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,gBAAgB,EACxB,KAAK,EAAE,MAAM,KACV,IAAI,CAAC;IACV,qBAAq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AO,CAAC,OAAO,CAAC;IAIlD,eAAe,CACb,SAAS,EAAE,YAAY,EAAE,EACzB,OAAO,EAAE;QACP,eAAe,CAAC,EAAE,MAAM,CAAC;QACzB,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;KACjB,GACA,YAAY,EAAE;IAuBX,gBAAgB,CAAC,QAAQ,EAAE,YAAY,GAAG,OAAO,CAAC,gBAAgB,CAAC;IA6IzE;;OAEG;IACG,QAAQ,CAAC,SAAS,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC,gBAAgB,CAAC;CA8ErE"}
|
package/dist/evaluator.js
CHANGED
|
@@ -90,7 +90,7 @@ class Evaluator extends TypedEventEmitter_1.TypedEventEmitter {
|
|
|
90
90
|
if (this.options.verbose && patternResult.score >= 0) {
|
|
91
91
|
this.emit("log", ` Pattern validation: ${patternResult.score.toFixed(2)}`);
|
|
92
92
|
}
|
|
93
|
-
const llmValidator = new llmJudge_1.LLMJudgeValidator(this.workspaceRoot, this.options.
|
|
93
|
+
const llmValidator = new llmJudge_1.LLMJudgeValidator(this.workspaceRoot, this.options.judgeModel);
|
|
94
94
|
const llmResult = await llmValidator.validate(generatedFiles, scenario);
|
|
95
95
|
validationResults.push(llmResult);
|
|
96
96
|
if (this.options.verbose && llmResult.score >= 0) {
|
package/dist/evaluator.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"evaluator.js","sourceRoot":"","sources":["../src/evaluator.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAUH,sDAA0D;AAC1D,4DAAgE;AAChE,oEAAiE;AACjE,oDAA0D;AAC1D,kEAA+D;AAC/D,2DAA8D;AAC9D,6DAA0D;AAC1D,iEAA8D;AAC9D,iDAAyC;
|
|
1
|
+
{"version":3,"file":"evaluator.js","sourceRoot":"","sources":["../src/evaluator.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAUH,sDAA0D;AAC1D,4DAAgE;A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CAAC,GAAG,GAAG,CAAC,CAAC,UAAU,CAAC,MAAM,EACrC,CAAC,CACF,CAAC;QACF,MAAM,YAAY,GAChB,OAAO,CAAC,MAAM,GAAG,CAAC;YAChB,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,MAAM;YAC/D,CAAC,CAAC,CAAC,CAAC;QAER,MAAM,MAAM,GAAqB;YAC/B,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO;YAC7B,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,KAAK;YACzB,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;YACnC,OAAO;YACP,OAAO,EAAE;gBACP,KAAK,EAAE,SAAS,CAAC,MAAM;gBACvB,MAAM;gBACN,MAAM;gBACN,OAAO;gBACP,YAAY;gBACZ,eAAe;aAChB;YACD,aAAa,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS;SACtC,CAAC;QAEF,IAAI,CAAC,IAAI,CAAC,qBAAqB,EAAE,MAAM,CAAC,CAAC;QAEzC,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AAnSD,8BAmSC"}
|
package/dist/lcs.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Longest Common Subsequence (LCS) Algorithm
|
|
3
|
+
*
|
|
4
|
+
* Finds the longest subsequence common to two strings using dynamic programming.
|
|
5
|
+
* A subsequence is a sequence that appears in the same relative order, but not
|
|
6
|
+
* necessarily contiguous.
|
|
7
|
+
*/
|
|
8
|
+
interface LCSResult {
|
|
9
|
+
readonly length: number;
|
|
10
|
+
readonly subsequence: string;
|
|
11
|
+
readonly matrix: readonly (readonly number[])[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Computes the longest common subsequence between two strings
|
|
15
|
+
*
|
|
16
|
+
* Time Complexity: O(m * n) where m and n are the lengths of the input strings
|
|
17
|
+
* Space Complexity: O(m * n) for the DP table
|
|
18
|
+
*/
|
|
19
|
+
export declare const findLongestCommonSubsequence: (str1: string, str2: string) => LCSResult;
|
|
20
|
+
/**
|
|
21
|
+
* Computes only the length of the LCS with optimized space complexity
|
|
22
|
+
*
|
|
23
|
+
* Time Complexity: O(m * n)
|
|
24
|
+
* Space Complexity: O(min(m, n)) - uses only two rows instead of full matrix
|
|
25
|
+
*/
|
|
26
|
+
export declare const findLCSLengthOptimized: (str1: string, str2: string) => number;
|
|
27
|
+
/**
|
|
28
|
+
* Finds all possible longest common subsequences between two strings
|
|
29
|
+
* Note: There can be multiple LCS of the same length
|
|
30
|
+
*/
|
|
31
|
+
export declare const findAllLCS: (str1: string, str2: string) => readonly string[];
|
|
32
|
+
export {};
|
|
33
|
+
//# sourceMappingURL=lcs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lcs.d.ts","sourceRoot":"","sources":["../src/lcs.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,UAAU,SAAS;IACjB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,CAAC;CACjD;AAED;;;;;GAKG;AACH,eAAO,MAAM,4BAA4B,GAAI,MAAM,MAAM,EAAE,MAAM,MAAM,KAAG,SA+BzE,CAAC;AAiCF;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,GAAI,MAAM,MAAM,EAAE,MAAM,MAAM,KAAG,MAwBnE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,UAAU,GAAI,MAAM,MAAM,EAAE,MAAM,MAAM,KAAG,SAAS,MAAM,EAyCtE,CAAC"}
|
package/dist/lcs.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Longest Common Subsequence (LCS) Algorithm
|
|
4
|
+
*
|
|
5
|
+
* Finds the longest subsequence common to two strings using dynamic programming.
|
|
6
|
+
* A subsequence is a sequence that appears in the same relative order, but not
|
|
7
|
+
* necessarily contiguous.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.findAllLCS = exports.findLCSLengthOptimized = exports.findLongestCommonSubsequence = void 0;
|
|
11
|
+
/**
|
|
12
|
+
* Computes the longest common subsequence between two strings
|
|
13
|
+
*
|
|
14
|
+
* Time Complexity: O(m * n) where m and n are the lengths of the input strings
|
|
15
|
+
* Space Complexity: O(m * n) for the DP table
|
|
16
|
+
*/
|
|
17
|
+
const findLongestCommonSubsequence = (str1, str2) => {
|
|
18
|
+
const m = str1.length;
|
|
19
|
+
const n = str2.length;
|
|
20
|
+
// Initialize DP table with dimensions (m+1) x (n+1)
|
|
21
|
+
// dp[i][j] represents the length of LCS for str1[0...i-1] and str2[0...j-1]
|
|
22
|
+
const dp = Array(m + 1)
|
|
23
|
+
.fill(null)
|
|
24
|
+
.map(() => Array(n + 1).fill(0));
|
|
25
|
+
// Build the DP table bottom-up
|
|
26
|
+
for (let i = 1; i <= m; i++) {
|
|
27
|
+
for (let j = 1; j <= n; j++) {
|
|
28
|
+
if (str1[i - 1] === str2[j - 1]) {
|
|
29
|
+
// Characters match: extend the LCS by 1
|
|
30
|
+
dp[i][j] = dp[i - 1][j - 1] + 1;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
// Characters don't match: take the maximum from either direction
|
|
34
|
+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// Backtrack through the DP table to construct the actual subsequence
|
|
39
|
+
const subsequence = reconstructSubsequence(str1, str2, dp);
|
|
40
|
+
return {
|
|
41
|
+
length: dp[m][n],
|
|
42
|
+
subsequence,
|
|
43
|
+
matrix: dp,
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
exports.findLongestCommonSubsequence = findLongestCommonSubsequence;
|
|
47
|
+
/**
|
|
48
|
+
* Reconstructs the actual LCS string by backtracking through the DP table
|
|
49
|
+
*/
|
|
50
|
+
const reconstructSubsequence = (str1, str2, dp) => {
|
|
51
|
+
const result = [];
|
|
52
|
+
let i = str1.length;
|
|
53
|
+
let j = str2.length;
|
|
54
|
+
// Traverse from bottom-right to top-left
|
|
55
|
+
while (i > 0 && j > 0) {
|
|
56
|
+
if (str1[i - 1] === str2[j - 1]) {
|
|
57
|
+
// Character is part of LCS: add to result and move diagonally
|
|
58
|
+
result.unshift(str1[i - 1]);
|
|
59
|
+
i--;
|
|
60
|
+
j--;
|
|
61
|
+
}
|
|
62
|
+
else if (dp[i - 1][j] > dp[i][j - 1]) {
|
|
63
|
+
// Move up (exclude current character from str1)
|
|
64
|
+
i--;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
// Move left (exclude current character from str2)
|
|
68
|
+
j--;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return result.join('');
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Computes only the length of the LCS with optimized space complexity
|
|
75
|
+
*
|
|
76
|
+
* Time Complexity: O(m * n)
|
|
77
|
+
* Space Complexity: O(min(m, n)) - uses only two rows instead of full matrix
|
|
78
|
+
*/
|
|
79
|
+
const findLCSLengthOptimized = (str1, str2) => {
|
|
80
|
+
// Ensure str2 is the shorter string to minimize space usage
|
|
81
|
+
if (str1.length < str2.length) {
|
|
82
|
+
[str1, str2] = [str2, str1];
|
|
83
|
+
}
|
|
84
|
+
const n = str2.length;
|
|
85
|
+
let prev = Array(n + 1).fill(0);
|
|
86
|
+
let curr = Array(n + 1).fill(0);
|
|
87
|
+
for (let i = 1; i <= str1.length; i++) {
|
|
88
|
+
for (let j = 1; j <= n; j++) {
|
|
89
|
+
if (str1[i - 1] === str2[j - 1]) {
|
|
90
|
+
curr[j] = prev[j - 1] + 1;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
curr[j] = Math.max(prev[j], curr[j - 1]);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// Swap rows: current becomes previous for next iteration
|
|
97
|
+
[prev, curr] = [curr, prev];
|
|
98
|
+
curr.fill(0);
|
|
99
|
+
}
|
|
100
|
+
return prev[n];
|
|
101
|
+
};
|
|
102
|
+
exports.findLCSLengthOptimized = findLCSLengthOptimized;
|
|
103
|
+
/**
|
|
104
|
+
* Finds all possible longest common subsequences between two strings
|
|
105
|
+
* Note: There can be multiple LCS of the same length
|
|
106
|
+
*/
|
|
107
|
+
const findAllLCS = (str1, str2) => {
|
|
108
|
+
const m = str1.length;
|
|
109
|
+
const n = str2.length;
|
|
110
|
+
// Build DP table
|
|
111
|
+
const dp = Array(m + 1)
|
|
112
|
+
.fill(null)
|
|
113
|
+
.map(() => Array(n + 1).fill(0));
|
|
114
|
+
for (let i = 1; i <= m; i++) {
|
|
115
|
+
for (let j = 1; j <= n; j++) {
|
|
116
|
+
if (str1[i - 1] === str2[j - 1]) {
|
|
117
|
+
dp[i][j] = dp[i - 1][j - 1] + 1;
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
// Backtrack to find all possible LCS
|
|
125
|
+
const results = new Set();
|
|
126
|
+
const backtrack = (i, j, current) => {
|
|
127
|
+
if (i === 0 || j === 0) {
|
|
128
|
+
results.add(current.split('').reverse().join(''));
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (str1[i - 1] === str2[j - 1]) {
|
|
132
|
+
backtrack(i - 1, j - 1, current + str1[i - 1]);
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
if (dp[i - 1][j] >= dp[i][j - 1]) {
|
|
136
|
+
backtrack(i - 1, j, current);
|
|
137
|
+
}
|
|
138
|
+
if (dp[i][j - 1] >= dp[i - 1][j]) {
|
|
139
|
+
backtrack(i, j - 1, current);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
backtrack(m, n, '');
|
|
144
|
+
return Array.from(results);
|
|
145
|
+
};
|
|
146
|
+
exports.findAllLCS = findAllLCS;
|
|
147
|
+
//# sourceMappingURL=lcs.js.map
|
package/dist/lcs.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lcs.js","sourceRoot":"","sources":["../src/lcs.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAQH;;;;;GAKG;AACI,MAAM,4BAA4B,GAAG,CAAC,IAAY,EAAE,IAAY,EAAa,EAAE;IACpF,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC;IACtB,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC;IAEtB,oDAAoD;IACpD,4EAA4E;IAC5E,MAAM,EAAE,GAAe,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC;SAChC,IAAI,CAAC,IAAI,CAAC;SACV,GAAG,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAEnC,+BAA+B;IAC/B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC5B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YAC5B,IAAI,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;gBAChC,wCAAwC;gBACxC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC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EAAE,OAAO,CAAC,CAAC;YAC/B,CAAC;QACH,CAAC;IACH,CAAC,CAAC;IAEF,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;IACpB,OAAO,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AAC7B,CAAC,CAAC;AAzCW,QAAA,UAAU,cAyCrB"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Result of the Longest Common Subsequence algorithm
|
|
3
|
+
*/
|
|
4
|
+
export interface LCSResult {
|
|
5
|
+
readonly subsequence: string;
|
|
6
|
+
readonly length: number;
|
|
7
|
+
readonly table: readonly (readonly number[])[];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Finds the longest common subsequence (LCS) between two strings using dynamic programming.
|
|
11
|
+
*
|
|
12
|
+
* Time Complexity: O(m * n) where m and n are the lengths of the input strings
|
|
13
|
+
* Space Complexity: O(m * n) for the DP table
|
|
14
|
+
*
|
|
15
|
+
* @param str1 - First string
|
|
16
|
+
* @param str2 - Second string
|
|
17
|
+
* @returns LCS result containing the subsequence, its length, and the DP table
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const result = findLongestCommonSubsequence("ABCDGH", "AEDFHR");
|
|
22
|
+
* console.log(result.subsequence); // "ADH"
|
|
23
|
+
* console.log(result.length); // 3
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare const findLongestCommonSubsequence: (str1: string, str2: string) => LCSResult;
|
|
27
|
+
/**
|
|
28
|
+
* Computes only the length of the LCS with space-optimized approach.
|
|
29
|
+
*
|
|
30
|
+
* This version uses O(min(m, n)) space instead of O(m * n) by keeping
|
|
31
|
+
* only the current and previous rows of the DP table.
|
|
32
|
+
*
|
|
33
|
+
* Useful when only the length is needed, not the actual subsequence.
|
|
34
|
+
*/
|
|
35
|
+
export declare const findLCSLength: (str1: string, str2: string) => number;
|
|
36
|
+
/**
|
|
37
|
+
* Finds all longest common subsequences between two strings.
|
|
38
|
+
*
|
|
39
|
+
* Note: There can be multiple LCS of the same length. This function
|
|
40
|
+
* finds all unique subsequences with the maximum length.
|
|
41
|
+
*
|
|
42
|
+
* Warning: This can be exponential in complexity for strings with many LCS.
|
|
43
|
+
*/
|
|
44
|
+
export declare const findAllLCS: (str1: string, str2: string) => readonly string[];
|
|
45
|
+
//# sourceMappingURL=longest-common-subsequence.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"longest-common-subsequence.d.ts","sourceRoot":"","sources":["../src/longest-common-subsequence.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,CAAC;CAChD;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,4BAA4B,GAAI,MAAM,MAAM,EAAE,MAAM,MAAM,KAAG,SAezE,CAAC;AAqEF;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa,GAAI,MAAM,MAAM,EAAE,MAAM,MAAM,KAAG,MAyB1D,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,UAAU,GAAI,MAAM,MAAM,EAAE,MAAM,MAAM,KAAG,SAAS,MAAM,EA+BtE,CAAC"}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.findAllLCS = exports.findLCSLength = exports.findLongestCommonSubsequence = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Finds the longest common subsequence (LCS) between two strings using dynamic programming.
|
|
6
|
+
*
|
|
7
|
+
* Time Complexity: O(m * n) where m and n are the lengths of the input strings
|
|
8
|
+
* Space Complexity: O(m * n) for the DP table
|
|
9
|
+
*
|
|
10
|
+
* @param str1 - First string
|
|
11
|
+
* @param str2 - Second string
|
|
12
|
+
* @returns LCS result containing the subsequence, its length, and the DP table
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const result = findLongestCommonSubsequence("ABCDGH", "AEDFHR");
|
|
17
|
+
* console.log(result.subsequence); // "ADH"
|
|
18
|
+
* console.log(result.length); // 3
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
const findLongestCommonSubsequence = (str1, str2) => {
|
|
22
|
+
const m = str1.length;
|
|
23
|
+
const n = str2.length;
|
|
24
|
+
// Build DP table: table[i][j] represents LCS length of str1[0...i-1] and str2[0...j-1]
|
|
25
|
+
const table = buildLCSTable(str1, str2, m, n);
|
|
26
|
+
// Backtrack through the table to reconstruct the actual subsequence
|
|
27
|
+
const subsequence = reconstructSubsequence(str1, str2, table, m, n);
|
|
28
|
+
return {
|
|
29
|
+
subsequence,
|
|
30
|
+
length: table[m][n],
|
|
31
|
+
table,
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
exports.findLongestCommonSubsequence = findLongestCommonSubsequence;
|
|
35
|
+
/**
|
|
36
|
+
* Builds the dynamic programming table for LCS computation.
|
|
37
|
+
*
|
|
38
|
+
* The table uses a bottom-up approach where:
|
|
39
|
+
* - table[i][j] = 0 if either string is empty (i=0 or j=0)
|
|
40
|
+
* - table[i][j] = table[i-1][j-1] + 1 if characters match
|
|
41
|
+
* - table[i][j] = max(table[i-1][j], table[i][j-1]) if characters don't match
|
|
42
|
+
*/
|
|
43
|
+
const buildLCSTable = (str1, str2, m, n) => {
|
|
44
|
+
const table = Array(m + 1)
|
|
45
|
+
.fill(null)
|
|
46
|
+
.map(() => Array(n + 1).fill(0));
|
|
47
|
+
// Fill the table using bottom-up dynamic programming
|
|
48
|
+
for (let i = 1; i <= m; i++) {
|
|
49
|
+
for (let j = 1; j <= n; j++) {
|
|
50
|
+
if (str1[i - 1] === str2[j - 1]) {
|
|
51
|
+
// Characters match: extend the LCS from the previous diagonal cell
|
|
52
|
+
table[i][j] = table[i - 1][j - 1] + 1;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
// Characters don't match: take the maximum from top or left cell
|
|
56
|
+
table[i][j] = Math.max(table[i - 1][j], table[i][j - 1]);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return table;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Reconstructs the actual subsequence by backtracking through the DP table.
|
|
64
|
+
*
|
|
65
|
+
* Backtracking logic:
|
|
66
|
+
* - If characters match, include the character and move diagonally up-left
|
|
67
|
+
* - If top cell > left cell, move up
|
|
68
|
+
* - Otherwise, move left
|
|
69
|
+
*/
|
|
70
|
+
const reconstructSubsequence = (str1, str2, table, m, n) => {
|
|
71
|
+
const result = [];
|
|
72
|
+
let i = m;
|
|
73
|
+
let j = n;
|
|
74
|
+
// Backtrack from bottom-right to top-left
|
|
75
|
+
while (i > 0 && j > 0) {
|
|
76
|
+
if (str1[i - 1] === str2[j - 1]) {
|
|
77
|
+
// Character is part of LCS: add to result and move diagonally
|
|
78
|
+
result.unshift(str1[i - 1]);
|
|
79
|
+
i--;
|
|
80
|
+
j--;
|
|
81
|
+
}
|
|
82
|
+
else if (table[i - 1][j] > table[i][j - 1]) {
|
|
83
|
+
// Move up (str1 contributes more to LCS at this point)
|
|
84
|
+
i--;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
// Move left (str2 contributes more to LCS at this point)
|
|
88
|
+
j--;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return result.join('');
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Computes only the length of the LCS with space-optimized approach.
|
|
95
|
+
*
|
|
96
|
+
* This version uses O(min(m, n)) space instead of O(m * n) by keeping
|
|
97
|
+
* only the current and previous rows of the DP table.
|
|
98
|
+
*
|
|
99
|
+
* Useful when only the length is needed, not the actual subsequence.
|
|
100
|
+
*/
|
|
101
|
+
const findLCSLength = (str1, str2) => {
|
|
102
|
+
// Ensure str1 is the shorter string to minimize space usage
|
|
103
|
+
if (str1.length > str2.length) {
|
|
104
|
+
[str1, str2] = [str2, str1];
|
|
105
|
+
}
|
|
106
|
+
const m = str1.length;
|
|
107
|
+
const n = str2.length;
|
|
108
|
+
let prev = Array(n + 1).fill(0);
|
|
109
|
+
let curr = Array(n + 1).fill(0);
|
|
110
|
+
for (let i = 1; i <= m; i++) {
|
|
111
|
+
for (let j = 1; j <= n; j++) {
|
|
112
|
+
if (str1[i - 1] === str2[j - 1]) {
|
|
113
|
+
curr[j] = prev[j - 1] + 1;
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
curr[j] = Math.max(prev[j], curr[j - 1]);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
// Swap arrays: current row becomes previous for next iteration
|
|
120
|
+
[prev, curr] = [curr, prev];
|
|
121
|
+
}
|
|
122
|
+
return prev[n];
|
|
123
|
+
};
|
|
124
|
+
exports.findLCSLength = findLCSLength;
|
|
125
|
+
/**
|
|
126
|
+
* Finds all longest common subsequences between two strings.
|
|
127
|
+
*
|
|
128
|
+
* Note: There can be multiple LCS of the same length. This function
|
|
129
|
+
* finds all unique subsequences with the maximum length.
|
|
130
|
+
*
|
|
131
|
+
* Warning: This can be exponential in complexity for strings with many LCS.
|
|
132
|
+
*/
|
|
133
|
+
const findAllLCS = (str1, str2) => {
|
|
134
|
+
const m = str1.length;
|
|
135
|
+
const n = str2.length;
|
|
136
|
+
const table = buildLCSTable(str1, str2, m, n);
|
|
137
|
+
const results = new Set();
|
|
138
|
+
const backtrack = (i, j, current) => {
|
|
139
|
+
// Base case: reached the beginning
|
|
140
|
+
if (i === 0 || j === 0) {
|
|
141
|
+
results.add(current);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (str1[i - 1] === str2[j - 1]) {
|
|
145
|
+
// Characters match: this character must be in all LCS paths
|
|
146
|
+
backtrack(i - 1, j - 1, str1[i - 1] + current);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
// Characters don't match: explore both possible paths
|
|
150
|
+
if (table[i - 1][j] === table[i][j]) {
|
|
151
|
+
backtrack(i - 1, j, current);
|
|
152
|
+
}
|
|
153
|
+
if (table[i][j - 1] === table[i][j]) {
|
|
154
|
+
backtrack(i, j - 1, current);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
backtrack(m, n, '');
|
|
159
|
+
return Array.from(results);
|
|
160
|
+
};
|
|
161
|
+
exports.findAllLCS = findAllLCS;
|
|
162
|
+
//# sourceMappingURL=longest-common-subsequence.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"longest-common-subsequence.js","sourceRoot":"","sources":["../src/longest-common-subsequence.ts"],"names":[],"mappings":";;;AASA;;;;;;;;;;;;;;;;GAgBG;AACI,MAAM,4BAA4B,GAAG,CAAC,IAAY,EAAE,IAAY,EAAa,EAAE;IACpF,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC;IACtB,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC;IAEtB,uFAAuF;IACvF,MAAM,KAAK,GAAG,aAAa,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IAE9C,oEAAoE;IACpE,MAAM,WAAW,GAAG,sBAAsB,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IAEpE,OAAO;QACL,WAAW;QACX,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACnB,KAAK;KACN,CAAC;AACJ,CAAC,CAAC;AAfW,QAAA,4BAA4B,gCAevC;AAEF;;;;;;;GAOG;AACH,MAAM,aAAa,GAAG,CAAC,IAAY,EAAE,IAAY,EAAE,CAAS,EAA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OAAO,CAAC,CAAC;YACrB,OAAO;QACT,CAAC;QAED,IAAI,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;YAChC,4DAA4D;YAC5D,SAAS,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC;QACjD,CAAC;aAAM,CAAC;YACN,sDAAsD;YACtD,IAAI,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;gBACpC,SAAS,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,OAAO,CAAC,CAAC;YAC/B,CAAC;YACD,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;gBACpC,SAAS,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,CAAC;YAC/B,CAAC;QACH,CAAC;IACH,CAAC,CAAC;IAEF,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;IAEpB,OAAO,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AAC7B,CAAC,CAAC;AA/BW,QAAA,UAAU,cA+BrB"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Result of the longest common subsequence computation
|
|
3
|
+
*/
|
|
4
|
+
interface LCSResult {
|
|
5
|
+
readonly length: number;
|
|
6
|
+
readonly subsequence: string;
|
|
7
|
+
readonly dpTable: ReadonlyArray<ReadonlyArray<number>>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Finds the longest common subsequence between two strings using dynamic programming.
|
|
11
|
+
*
|
|
12
|
+
* Time Complexity: O(m * n) where m and n are the lengths of the input strings
|
|
13
|
+
* Space Complexity: O(m * n) for the DP table
|
|
14
|
+
*
|
|
15
|
+
* @param str1 - First input string
|
|
16
|
+
* @param str2 - Second input string
|
|
17
|
+
* @returns LCS result containing length, subsequence, and DP table
|
|
18
|
+
*/
|
|
19
|
+
export declare const longestCommonSubsequence: (str1: string, str2: string) => LCSResult;
|
|
20
|
+
/**
|
|
21
|
+
* Finds all possible longest common subsequences between two strings.
|
|
22
|
+
* Note: There may be multiple LCS of the same length.
|
|
23
|
+
*
|
|
24
|
+
* @param str1 - First input string
|
|
25
|
+
* @param str2 - Second input string
|
|
26
|
+
* @returns Array of all possible LCS strings
|
|
27
|
+
*/
|
|
28
|
+
export declare const findAllLCS: (str1: string, str2: string) => readonly string[];
|
|
29
|
+
/**
|
|
30
|
+
* Computes LCS length with space optimization (O(min(m,n)) space).
|
|
31
|
+
* Only returns the length, not the actual subsequence.
|
|
32
|
+
*
|
|
33
|
+
* @param str1 - First input string
|
|
34
|
+
* @param str2 - Second input string
|
|
35
|
+
* @returns Length of the LCS
|
|
36
|
+
*/
|
|
37
|
+
export declare const lcsLengthOptimized: (str1: string, str2: string) => number;
|
|
38
|
+
export {};
|
|
39
|
+
//# sourceMappingURL=longestCommonSubsequence.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"longestCommonSubsequence.d.ts","sourceRoot":"","sources":["../src/longestCommonSubsequence.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,UAAU,SAAS;IACjB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC;CACxD;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,wBAAwB,GAAI,MAAM,MAAM,EAAE,MAAM,MAAM,KAAG,SA8BrE,CAAC;AAkCF;;;;;;;GAOG;AACH,eAAO,MAAM,UAAU,GAAI,MAAM,MAAM,EAAE,MAAM,MAAM,KAAG,SAAS,MAAM,EAsBtE,CAAC;AA0CF;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,GAAI,MAAM,MAAM,EAAE,MAAM,MAAM,KAAG,MAwB/D,CAAC"}
|