Shortest Common Supersequence
Real-world version: The Shortest Tape Holding Both ReadingsThe statement here is our own: the same inputs, outputs and technique.
Two readings of lowercase letters are given, first and second. A tape holds a reading when deleting some of the tape's letters, possibly none of them, leaves that reading exactly.
Return the shortest tape that holds both readings. Where several tapes tie for shortest, return the one that comes first in dictionary order.
Examples
Example 1
- Input
- first = "bcd", second = "abc"
- Output
- "abcd"
The tape holds the first reading once its opening letter is dropped and the second once its closing letter is dropped. Nothing shorter works, since the two readings share only the letters b and c.
Example 2
- Input
- first = "abc", second = "def"
- Output
- "abcdef"
The two readings share no letter at all, so the tape has to hold all six. Writing the first reading before the second comes earlier in dictionary order than the other way round.
Example 3
- Input
- first = "ab", second = "ba"
- Output
- "aba"
Three letters are needed, since the readings share only one letter in common. Both possible tapes work, and the one opening on a comes first in dictionary order.
Constraints
- 1 <= first.length <= 1000
- 1 <= second.length <= 1000
- Both readings are made of lowercase English letters.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def shortest_common_supersequence(first: str, second: str) -> str:public String shortestCommonSupersequence(String first, String second)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's