Dynamic programming · Medium
A comparison tool summarizes two short event-code strings by the longest sequence of codes they share in the same relative order. Unmatched codes may be skipped. The shared sequence does not need to be contiguous in either input.
Return the length of the longest common subsequence while preserving relative order in both strings.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ a: string, b: string }
Fields
- a and b are case-sensitive printable ASCII strings. Spaces are ordinary characters.
Output
Return value
- Return an integer length, not a substring or the sequence itself.
- Characters may be skipped but never reordered. An empty operand gives zero.
Constraints
- 0 <= a.length, b.length <= 1000.
Examples
Example 1
{
"a": "abcde",
"b": "ace"
}Returns
3Example 2
{
"a": "aa",
"b": "a"
}Returns
1Required complexity
O(nm) time and O(min(n,m) + 1) auxiliary space