Dynamic programming · Hard
Two devices have ordered action histories. A server wants the shortest replay containing both histories as subsequences, with deterministic text on ties.
Return the lexicographically smallest shortest common supersequence.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ a:string, b:string }
Input rules
- Each lowercase ASCII character is an action kind.
What each field means
aandb— action sequences; each lowercase character is one action occurrence.
Output
Return value and rules
- Return a shortest string that contains both inputs as subsequences.
- Each input must retain its order, but may skip characters in the returned string.
- Among equally short answers, choose the smallest in ASCII lexicographic order.
Constraints
- 0 <= a.length, b.length <= 500
- Repeated characters represent separate actions; they must not be deduplicated globally.
Examples
Example 1
{
"a": "ab",
"b": "ac"
}Returns
"abc"Example 2
{
"a": "",
"b": ""
}Returns
""Required complexity
O(nm) time and O(nm) space