Longest Uncommon Subsequence I - Problem

Given two strings a and b, find the longest uncommon subsequence between them. An uncommon subsequence is a string that is a subsequence of exactly one of the two strings (not both).

A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

Goal: Return the length of the longest uncommon subsequence. If no such uncommon subsequence exists, return -1.

Key Insight: Think about what makes a subsequence "uncommon" - it must belong to one string but not the other!

Input & Output

example_1.py โ€” Different Strings
$ Input: a = "aba", b = "cdc"
โ€บ Output: 3
๐Ÿ’ก Note: Since the strings are different, each entire string is a subsequence of exactly one string. The longest uncommon subsequence is either "aba" or "cdc", both with length 3.
example_2.py โ€” Identical Strings
$ Input: a = "aaa", b = "aaa"
โ€บ Output: -1
๐Ÿ’ก Note: Since both strings are identical, any subsequence of one string will also be a subsequence of the other. Therefore, no uncommon subsequence exists.
example_3.py โ€” Different Lengths
$ Input: a = "a", b = "aa"
โ€บ Output: 2
๐Ÿ’ก Note: The strings are different, so each string itself is an uncommon subsequence. The longer string "aa" has length 2, which is the maximum.

Constraints

  • 1 โ‰ค a.length, b.length โ‰ค 100
  • a and b consist of lower-case English letters
  • Key insight: The maximum possible answer is max(a.length, b.length)

Visualization

Tap to expand
Longest Uncommon Subsequence: The LogicCase 1: a == bExample: a = "abc", b = "abc"Any subsequence of a:"a", "ab", "abc", etc.Is also a subsequence of b!No uncommon subsequence existsResult: -1Case 2: a != bExample: a = "abc", b = "def"String "abc" is:โœ“ A subsequence of aโœ— NOT a subsequence of b"abc" is an uncommon subsequence!Result: max(len(a), len(b))๐Ÿ’ก The Key InsightIf two strings are different, each string itself is a subsequence of exactly one of them.Since we want the LONGEST uncommon subsequence, and no subsequence can belonger than the original string, the answer is simply the length of the longer string!Optimal Solution: O(n) Time, O(1) Spacereturn a == b ? -1 : Math.max(a.length, b.length);
Understanding the Visualization
1
Case 1: Identical Strings
If a == b, any subsequence of a is also a subsequence of b, so no uncommon subsequence exists
2
Case 2: Different Strings
If a != b, then string a is a subsequence of a but NOT of b (since they're different)
3
Key Insight
Each entire string is itself the longest possible uncommon subsequence!
Key Takeaway
๐ŸŽฏ Key Insight: If strings are different, each string is its own longest uncommon subsequence!
Asked in
Google 15 Amazon 8 Microsoft 5 Apple 3
24.5K Views
Medium Frequency
~5 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen