Longest Common Subsequence
Real-world version: Shared Checkpoint OrderThe statement here is our own: the same inputs, outputs and technique.
Two field technicians walked the same plant during one shift. Each kept a personal log of the maintenance checkpoints they signed off, written in the order they signed them. Every checkpoint is identified by a single lowercase letter, and a technician may sign the same checkpoint again later in the shift.
Call a trace of a log whatever is left once you erase any number of its entries, including erasing nothing at all; the entries you keep stay in the sequence they were written.
Given the logs logA and logB, report how many entries the longest trace that occurs in both logs contains. Two logs that have no letter in common yield 0.
Examples
Example 1
- Input
- logA = "purge", logB = "urgent"
- Output
- 4
Erase the leading `p` from the first log and you are left with `urge`; those same four checkpoints sit in that order inside `urgent`.
Example 2
- Input
- logA = "bolt", logB = "vein"
- Output
- 0
The two technicians never touched the same checkpoint, so the longest trace they both hold is empty.
Example 3
- Input
- logA = "cot", logB = "carrot"
- Output
- 3
All three entries of the first log reappear in order inside the second, even though `a` and the two `r` entries sit between them.
Constraints
- 1 <= logA.length, logB.length <= 1000
- logA and logB hold lowercase letters only
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 longest_shared_trace(logA: str, logB: str) -> int:public int longestSharedTrace(String logA, String logB)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
More on the same topics
- Shortest Common Supersequence Real-world version: The Shortest Tape Holding Both ReadingsHard
- Is SubsequenceReal-world version: Shorthand Fits the LabelEasy
- Maximum Repeating SubstringReal-world version: Longest Run of a Repeated TagEasy
- Longest Palindromic SubstringReal-world version: Longest Mirror RunMedium