Dynamic programming · Hard
Two offline event histories must agree on a shared order. Reconciliation may delete events from either history but cannot reorder or insert them.
Return the minimum total number of deletions needed to make the histories identical.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ local:string, remote:string }
Input rules
- Each lowercase ASCII character represents an event kind; repetitions matter.
What each field means
localandremote— event histories; each character is one event occurrence.
Output
Return value and rules
- Return the fewest total character deletions needed to make both histories identical.
- You may delete from either history, but may not insert, replace or reorder characters.
- Return the deletion count, not the remaining history.
Constraints
- 0 <= local.length, remote.length <= 2000
- Use memory linear in the shorter history, not a quadratic table.
Examples
Example 1
{
"local": "abc",
"remote": "adc"
}Returns
2Example 2
{
"local": "",
"remote": ""
}Returns
0Required complexity
O(nm) time and O(min(n,m)) auxiliary space