809. Shortest Common Cover
A sign maker wants one banner that can show two slogans a and b. A banner shows a slogan if the slogan's letters appear on the banner in the same order, though other letters may sit in between them.
Return the length of the shortest banner (a string of lowercase letters) that shows both a and b in this sense. If one slogan can be read inside the other, the banner is simply the longer one.
Only the length is requested, not the banner itself. Aim for time O(n*m) and extra space O(m), where n and m are the lengths of a and b.
Example 1
- Input:
- a = "gridlock", b = "golden"
- Output:
- 12
- Explanation:
The two words share at most 2 letters in order (for example g then d), so the shortest cover has 8 + 6 - 2 = 12 letters.
Example 2
- Input:
- a = "kiwi", b = "wiki"
- Output:
- 6
- Explanation:
The words share the ordered pair w, i, giving 4 + 4 - 2 = 6; the string kiwiki contains both as subsequences.
Constraints
1 ≤ a.length, b.length ≤ 700
Both strings consist of lowercase English letters.
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,000 msC++ 250 msJava 500 msJavaScript 500 msTypeScript 500 ms
Expected complexity
- Time
- O(n*m)
- Space
- O(m)