Two field agents, Agent A and Agent B, each filed a dispatch consisting of lowercase English letters. An excerpt of a dispatch is any string you can obtain by deleting zero or more characters from it without reordering the characters that remain — that is, an excerpt is a subsequence of the dispatch.
Headquarters wants the length of the longest excerpt that is exclusive to one agent: it must be obtainable as an excerpt of one dispatch, but not obtainable as an excerpt of the other dispatch at all.
Report the length of the longest exclusive excerpt. If every excerpt obtainable from one dispatch is also obtainable from the other (this happens precisely when the two dispatches are identical), report -1 instead.
A single integer: the length of the longest exclusive excerpt, or -1 if no exclusive excerpt exists.
a-z).Example 1
Input
abc abc
Expected
-1
Explanation
The two dispatches are identical, so every excerpt obtainable from one is also obtainable from the other. No excerpt is exclusive, so the answer is -1.
Example 2
Input
aba cdc
Expected
3
Explanation
Both dispatches have length 3 but are different strings. The full dispatch A ("aba") cannot be an excerpt of B ("cdc", which shares no letters), so it is exclusive to A; symmetrically "cdc" is exclusive to B. The longest exclusive excerpt therefore has length 3.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →