Skip to content

fix: make recursive LCS memoization unambiguous and cache empty results - #2241

Open
Shubham-Padkonde wants to merge 1 commit into
trekhleb:masterfrom
Shubham-Padkonde:fix/lcs-recursive-memoization
Open

Shubham-Padkonde wants to merge 1 commit into
trekhleb:masterfrom
Shubham-Padkonde:fix/lcs-recursive-memoization

Conversation

@Shubham-Padkonde

Copy link
Copy Markdown

The recursive LCS memo key concatenates input suffixes with :, so different states collide when the strings contain colons. It also treats a cached empty subsequence as a miss, causing exponential repeated work for disjoint strings.

Key states by suffix lengths and check for undefined explicitly. Cache matching-character states too. ('::a:a', 'aa:::') now returns ::: instead of aa.

Fixes #2233.

Validation: both new regressions fail before the fix. The empty-result regression counts substring expansions instead of relying on wall-clock timing. All 178 suites / 589 tests and the full coverage check pass; the changed algorithm has 100% coverage. npm run lint passes.

AI-assisted contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

longestCommonSubsequenceRecursive: wrong result when strings contain ':' and exponential time when there is no common character

1 participant