Skip to content
← All posts

Longest Uncommon Subsequence I: The Trick Question

5 min read
leetcodeproblemeasystrings

LeetCode 521, Longest Uncommon Subsequence I, asks you to find the length of the longest uncommon subsequence between two strings a and b. An uncommon subsequence is defined as a string that is a subsequence of one but not a subsequence of the other. If no such subsequence exists, return -1.

A few examples:

  • a = "aba", b = "cdc" returns 3. The strings are different, so "aba" itself is a subsequence of a but not of b.
  • a = "aaa", b = "bbb" returns 3. Same logic. The strings differ, so the full string works.
  • a = "aaa", b = "aaa" returns -1. The strings are identical, so every subsequence of one is also a subsequence of the other.
Case 1: Strings are equalaabcbabca == breturn -1Case 2: Strings are differentaabcbabaa != breturn max(3, 3) = 3characters matchcharacters differ
When strings are equal, every subsequence of one is also a subsequence of the other, so no uncommon subsequence exists. When they differ, the longer string itself is the answer.

Why this is a trick question

This problem looks like it needs dynamic programming or some clever subsequence enumeration. It does not. The key insight comes from thinking about what "uncommon subsequence" actually means.

If a == b, then every subsequence of a is also a subsequence of b, and vice versa. There is no string that is a subsequence of one but not the other. Return -1.

If a != b, then the longer string itself is always a valid answer. Why? A string cannot be a subsequence of a shorter string (it would not fit). And if the two strings have the same length but different content, neither string is a subsequence of the other, because a string is only a subsequence of itself if the two strings are identical. Either way, max(len(a), len(b)) gives the correct answer.

That is the entire solution. Two conditions, two lines.

The code

def findLUSlength(a, b):
    if a == b:
        return -1
    return max(len(a), len(b))

The function checks string equality, and if the strings differ, returns the length of the longer one. No loops, no recursion, no extra data structures.

Step-by-step walkthrough

Let's walk through the three cases that cover all the logic.

Step 1: Check if a == b

result = -1
aabcbabc

Both strings are "abc". Every subsequence of a is also a subsequence of b (and vice versa). No uncommon subsequence exists. Return -1.

Step 2: Different strings, same length

result = 3
aabcbaba

The strings differ ("abc" != "aba"). Since "abc" is not a subsequence of "aba" (there is no 'c' in "aba"), the full string "abc" is an uncommon subsequence. Return max(3, 3) = 3.

Step 3: Different strings, different lengths

result = 4
aabbabcd

The strings differ. "abcd" has length 4 and cannot be a subsequence of "ab" (which is shorter). So "abcd" is an uncommon subsequence. Return max(2, 4) = 4.

Notice that every possible input falls into one of these scenarios. Equal strings always return -1. Unequal strings always return the maximum of their lengths. There are no other cases to consider.

Why the longer string works

It is worth pausing on why the longer string is always a valid uncommon subsequence when the strings differ.

Suppose len(a) >= len(b) and a != b. The string a is trivially a subsequence of itself. Could a also be a subsequence of b? For a to be a subsequence of b, every character of a would need to appear in order within b. But b is either shorter (so it physically cannot contain all of a) or the same length but different (so a would have to equal b to be a subsequence, which contradicts our assumption). In both cases, a is not a subsequence of b, making it an uncommon subsequence with length len(a).

This problem is a good reminder that the hardest part of some interview questions is resisting the urge to overcomplicate. Before writing code, ask yourself: "What does the answer actually depend on?" Sometimes the answer is simpler than the question suggests.

Complexity analysis

MetricValue
TimeO(n), where n is the length of the strings (for the equality check)
SpaceO(1), no extra data structures

The string equality check compares characters one by one, so it takes O(n) time in the worst case. Everything else is constant time.

Building blocks

This problem is built on one fundamental concept: string equality as a decision boundary.

The entire solution hinges on a single boolean check. If the strings match, the answer is one thing. If they do not match, the answer is another. No iteration over subsequences, no memoization tables, no character frequency maps.

This same "check equality first" pattern shows up in other problems:

  • Anagram detection: check if two strings have the same character frequencies before doing further work
  • Rotated string: check if two strings have the same length before checking if one is a rotation of the other
  • Subsequence matching: the simplest base case is checking if the strings are identical

The deeper skill here is recognizing when a problem that sounds complex actually reduces to a simple observation. LeetCode 521 is one of the purest examples of this.

Edge cases

Equal strings. a = "abc", b = "abc". Every subsequence of one is a subsequence of the other. Return -1.

Different lengths. a = "ab", b = "abcd". The longer string "abcd" cannot be a subsequence of the shorter string "ab". Return 4.

Single characters. a = "a", b = "b". The strings differ, so return max(1, 1) = 1.

Empty string. a = "", b = "abc". The strings differ (one is empty, one is not). Return max(0, 3) = 3. The non-empty string is a subsequence of itself but not of the empty string.

Both empty. a = "", b = "". The strings are equal. Return -1.

From understanding to recall

This problem is a great test of whether you truly understand what "subsequence" means. Most people who see it for the first time try to enumerate subsequences or build a DP table. The trick is realizing that none of that machinery is needed.

But knowing the trick once is not the same as remembering it under pressure. If you see a similar "sounds hard, is actually simple" problem in an interview six weeks from now, will the insight come to you immediately? Spaced repetition helps here. By revisiting the core reasoning ("equal means -1, different means max length") at increasing intervals, you build the kind of pattern recognition that makes these problems feel obvious instead of tricky.

Related posts