KatInterview

521. Longest Uncommon Subsequence I

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the length of the Longest Uncommon Subsequence (LUS) between two strings, a and b. An "uncommon subsequence" is defined as a string that is a subsequence of one of the given strings but not the other. If no such subsequence exists, we should return -1.

Solution

Longest Uncommon Subsequence I in Python

class Solution:
    def findLUSlength(self, a: str, b: str) -> int:
        # If the two strings are identical, any subsequence of one is also a subsequence of the other.
        # Therefore, no uncommon subsequence exists.
        if a == b:
            return -1
        
        # If the strings are not identical, one of them must be an uncommon subsequence.
        # Consider the longer string. It cannot be a subsequence of the shorter one.
        # For example, if a is longer than b, a is a subsequence of itself but not of b.
        # Thus, a is an uncommon subsequence. Its length is len(a).
        # No subsequence can be longer than the string from which it is derived.
        # Therefore, the length of the longer string is the length of the longest uncommon subsequence.
        # This also holds if they have the same length but are different, as neither can be a subsequence of the other.
        return max(len(a), len(b))

Longest Uncommon Subsequence I in Java

class Solution {
    public int findLUSlength(String a, String b) {
        // If the two strings are identical, any subsequence of one is also a subsequence of the other.
        // Therefore, no uncommon subsequence exists.
        if (a.equals(b)) {
            return -1;
        }
        
        // If the strings are not identical, one of them must be an uncommon subsequence.
        // Consider the longer string. It cannot be a subsequence of the shorter one.
        // For example, if a is longer than b, a is a subsequence of itself but not of b.
        // Thus, a is an uncommon subsequence. Its length is a.length().
        // No subsequence can be longer than the string from which it is derived.
        // Therefore, the length of the longer string is the length of the longest uncommon subsequence.
        // This also holds if they have the same length but are different, as neither can be a subsequence of the other.
        return Math.max(a.length(), b.length());
    }
}

Longest Uncommon Subsequence I in C++

class Solution {
public:
    int findLUSlength(string a, string b) {
        // If the two strings are identical, any subsequence of one is also a subsequence of the other.
        // Therefore, no uncommon subsequence exists.
        if (a == b) {
            return -1;
        }
        
        // If the strings are not identical, one of them must be an uncommon subsequence.
        // Consider the longer string. It cannot be a subsequence of the shorter one.
        // For example, if a is longer than b, a is a subsequence of itself but not of b.
        // Thus, a is an uncommon subsequence. Its length is a.length().
        // No subsequence can be longer than the string from which it is derived.
        // Therefore, the length of the longer string is the length of the longest uncommon subsequence.
        // This also holds if they have the same length but are different, as neither can be a subsequence of the other.
        return max(a.length(), b.length());
    }
};

Longest Uncommon Subsequence I in TypeScript

function findLUSlength(a: string, b: string): number {
    // If the two strings are identical, any subsequence of one is also a subsequence of the other.
    // Therefore, no uncommon subsequence exists.
    if (a === b) {
        return -1;
    }
    
    // If the strings are not identical, one of them must be an uncommon subsequence.
    // Consider the longer string. It cannot be a subsequence of the shorter one.
    // For example, if a is longer than b, a is a subsequence of itself but not of b.
    // Thus, a is an uncommon subsequence. Its length is a.length.
    // No subsequence can be longer than the string from which it is derived.
    // Therefore, the length of the longer string is the length of the longest uncommon subsequence.
    // This also holds if they have the same length but are different, as neither can be a subsequence of the other.
    return Math.max(a.length, b.length);
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.