{x}
blog image

Longest Uncommon Subsequence I

Given two strings a and b, return the length of the longest uncommon subsequence between a and b. If no such uncommon subsequence exists, return -1.

An uncommon subsequence between two strings is a string that is a subsequence of exactly one of them.

 

Example 1:

Input: a = "aba", b = "cdc"
Output: 3
Explanation: One longest uncommon subsequence is "aba" because "aba" is a subsequence of "aba" but not "cdc".
Note that "cdc" is also a longest uncommon subsequence.

Example 2:

Input: a = "aaa", b = "bbb"
Output: 3
Explanation: The longest uncommon subsequences are "aaa" and "bbb".

Example 3:

Input: a = "aaa", b = "aaa"
Output: -1
Explanation: Every subsequence of string a is also a subsequence of string b. Similarly, every subsequence of string b is also a subsequence of string a. So the answer would be -1.

 

Constraints:

  • 1 <= a.length, b.length <= 100
  • a and b consist of lower-case English letters.

Solution Explanation for Longest Uncommon Subsequence I

This problem asks to find the length of the longest uncommon subsequence between two given strings, a and b. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. An uncommon subsequence is a subsequence that exists in exactly one of the two strings.

The core observation is that if the two input strings a and b are identical, there's no uncommon subsequence; every subsequence of one is also a subsequence of the other. In this case, the function should return -1. Otherwise, the longest uncommon subsequence will simply be the longer of the two strings. This is because the longer string will always contain a subsequence (itself) that is not a subsequence of the shorter string.

Algorithm

  1. Check for Equality: Compare strings a and b. If they are identical (a == b), return -1.
  2. Return the Maximum Length: Otherwise, return the maximum of the lengths of a and b using max(len(a), len(b)) (or the equivalent in the chosen language). This represents the length of the longest uncommon subsequence.

Time and Space Complexity Analysis

  • Time Complexity: O(n), where n is the maximum length of the strings a and b. This is because string comparison and length calculation take linear time.

  • Space Complexity: O(1). The algorithm uses only a constant amount of extra space regardless of input size. No additional data structures are allocated that grow with input size.

Code Implementation in Multiple Languages

The solution is remarkably concise across different programming languages due to its simplicity. Here are implementations in several languages:

Python:

class Solution:
    def findLUSlength(self, a: str, b: str) -> int:
        return -1 if a == b else max(len(a), len(b))

Java:

class Solution {
    public int findLUSlength(String a, String b) {
        return a.equals(b) ? -1 : Math.max(a.length(), b.length());
    }
}

C++:

class Solution {
public:
    int findLUSlength(string a, string b) {
        return (a == b) ? -1 : max(a.length(), b.length());
    }
};

JavaScript:

const findLUSlength = (a, b) => {
    return a === b ? -1 : Math.max(a.length, b.length);
};

Go:

func findLUSlength(a string, b string) int {
    if a == b {
        return -1
    }
    return max(len(a), len(b))
}
 
func max(a, b int) int {
    if a > b {
        return a
    }
    return b
}

TypeScript:

function findLUSlength(a: string, b: string): number {
    return a === b ? -1 : Math.max(a.length, b.length);
}

Rust:

impl Solution {
    pub fn find_lu_slength(a: String, b: String) -> i32 {
        if a == b {
            -1
        } else {
            std::cmp::max(a.len(), b.len()) as i32
        }
    }
}

All these implementations directly reflect the algorithm: a simple conditional statement to check for string equality and a max function call to get the length of the longer string. The efficiency is excellent, making this an optimal solution for this problem.