Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

521. Longest Uncommon Subsequence I - Leetcode Solution

Problem Description

The "Longest Uncommon Subsequence I" problem asks you to compare two strings, a and b. An uncommon subsequence is defined as a string that is a subsequence of one string but not a subsequence of the other. Your task is to find the length of the longest such uncommon subsequence between a and b.

  • If no uncommon subsequence exists, return -1.
  • A subsequence is a sequence that can be derived from another string by deleting some (or no) characters without changing the order of the remaining characters.
  • Strings a and b are non-empty and can be up to 100 characters long.

Key constraints: Only a single valid answer exists for each input pair, and you do not need to consider reusing elements or generating all possible subsequences.

Thought Process

Let's break down the problem logically before jumping into code. The core idea is to find a string that is a subsequence of one input but not the other, and to maximize its length. At first glance, you might think about generating all possible subsequences of both strings and comparing them, but this would be highly inefficient given the exponential number of subsequences.

Instead, consider what happens when the two strings are equal or different:

  • If a and b are the same, every subsequence of a is also a subsequence of b (and vice versa). So, there is no uncommon subsequence.
  • If a and b are different, then the entire string a is not a subsequence of b (unless a is contained within b), and vice versa. So, the longer of the two strings is itself an uncommon subsequence.

This leads us to a much simpler solution than brute-force: just compare the two strings directly.

Solution Approach

Let's formalize the approach step-by-step:

  1. Check if the strings are equal:
    • If a == b, then no uncommon subsequence exists. Return -1.
  2. If the strings are not equal:
    • The longer of a or b cannot possibly be a subsequence of the other, so its entire length is the answer.
    • If they are of the same length but not equal, either string is a valid answer since neither is a subsequence of the other.
  3. Return the length of the longer string.

This approach is efficient because it avoids unnecessary computation and leverages the problem's constraints.

Example Walkthrough

Let's consider a few sample cases:

  • Example 1:
    a = "aba", b = "cdc"
    • Are the strings equal? No.
    • Lengths: both are 3.
    • Since they are not equal and have the same length, either string is an uncommon subsequence of the other.
    • Answer: 3
  • Example 2:
    a = "aaa", b = "aaa"
    • Are the strings equal? Yes.
    • No uncommon subsequence exists.
    • Answer: -1
  • Example 3:
    a = "abcd", b = "abc"
    • Are the strings equal? No.
    • Lengths: a is 4, b is 3.
    • a ("abcd") is not a subsequence of b ("abc"), so 4 is the answer.
    • Answer: 4

In each step, we simply check equality and compare lengths, making the process straightforward.

Time and Space Complexity

Brute-force approach:

  • Generating all possible subsequences of a string of length n takes O(2^n) time, which is infeasible for strings of length up to 100.
  • Checking each subsequence for being uncommon would multiply this cost.
Optimized approach (our solution):
  • Comparing two strings for equality: O(n), where n is the length of the strings.
  • Finding the length of the longer string: O(1).
  • Total Time Complexity: O(n)
  • Space Complexity: O(1) (no extra space used)

Our solution is highly efficient and scalable for the given constraints.

Summary

The key insight for solving "Longest Uncommon Subsequence I" is recognizing that if two strings are not equal, the longer one cannot be a subsequence of the other. This allows us to avoid brute-force generation of subsequences and instead simply compare string equality and length. The result is an elegant and efficient solution that runs in linear time and constant space.

Code Implementation

class Solution:
    def findLUSlength(self, a: str, b: str) -> int:
        if a == b:
            return -1
        else:
            return max(len(a), len(b))
      
class Solution {
public:
    int findLUSlength(string a, string b) {
        if (a == b)
            return -1;
        else
            return max(a.length(), b.length());
    }
};
      
class Solution {
    public int findLUSlength(String a, String b) {
        if (a.equals(b)) {
            return -1;
        } else {
            return Math.max(a.length(), b.length());
        }
    }
}
      
var findLUSlength = function(a, b) {
    if (a === b) {
        return -1;
    } else {
        return Math.max(a.length, b.length);
    }
};