Ctrl K

Subsequence Only One Has

Easy
A subsequence of a string is what remains after deleting some characters (possibly none) without changing the order of the rest. Given strings first and second, find the longest string that is a subsequence of exactly one of them: it can be obtained from one but not from the other. Return its length, or -1 if no such string exists.
Example 1
first="note", second="notebook" → 8

notebook cannot be obtained by deleting letters from the shorter note, so the whole 8-letter string works.

Example 2
first="plum", second="pear" → 4

plum cannot be made from pear or the other way round, so the answer is 4.

Example 3
first="echo", second="echo" → -1

The strings are equal, so anything one of them yields the other yields too.

Constraints
  • 1 ≤ len(first), len(second) ≤ 100
  • Both strings consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValue of a Roman Numeral EasyShared Start of Words EasyBracket Balance EasyWhere the Fragment Starts EasyLength of the Final Word Easy
loading Python…
Input
first="note", second="notebook"
Expected
8

Run checks the first 3 tests in your browser. Submit checks all 124 tests on the server, hidden ones included.