{x}
blog image

Longest Uncommon Subsequence II

Given an array of strings strs, return the length of the longest uncommon subsequence between them. If the longest uncommon subsequence does not exist, return -1.

An uncommon subsequence between an array of strings is a string that is a subsequence of one string but not the others.

A subsequence of a string s is a string that can be obtained after deleting any number of characters from s.

  • For example, "abc" is a subsequence of "aebdc" because you can delete the underlined characters in "aebdc" to get "abc". Other subsequences of "aebdc" include "aebdc", "aeb", and "" (empty string).

 

Example 1:

Input: strs = ["aba","cdc","eae"]
Output: 3

Example 2:

Input: strs = ["aaa","aaa","aa"]
Output: -1

 

Constraints:

  • 2 <= strs.length <= 50
  • 1 <= strs[i].length <= 10
  • strs[i] consists of lowercase English letters.

Solution Explanation: Longest Uncommon Subsequence II

The problem asks to find the length of the longest uncommon subsequence among a given array of strings. An uncommon subsequence is a string that's a subsequence of one string in the array but not a subsequence of any other string.

Approach:

The solution employs a brute-force approach with a helper function to check if one string is a subsequence of another. It iterates through each string in the input array:

  1. Subsequence Check (check(s, t)): This function uses a two-pointer approach to efficiently determine if string s is a subsequence of string t. It iterates through t, and if a character in s matches the current character in t, the pointer for s advances. If the pointer for s reaches the end of s, it means s is a subsequence of t.

  2. Uniqueness Check: For each string s, the code iterates through the rest of the strings in the array. If s is found to be a subsequence of any other string, it's not an uncommon subsequence, and its length is discarded.

  3. Longest Uncommon Subsequence: If a string s passes the uniqueness check (meaning it's not a subsequence of any other string in the array), its length is compared to the current maximum length (ans). The maximum length is updated if a longer uncommon subsequence is found.

  4. No Uncommon Subsequence: If no uncommon subsequence is found after iterating through all strings, the function returns -1.

Time Complexity Analysis:

  • The outer loop iterates through each string in the input array (n strings).
  • The inner loop iterates through the array again for each string in the outer loop.
  • The check function has a time complexity of O(m), where m is the maximum length of a string.

Therefore, the overall time complexity is O(n² * m), where n is the number of strings and m is the maximum length of a string. This is a brute-force solution, and its efficiency degrades significantly as the number of strings or their lengths increase.

Space Complexity Analysis:

The solution uses only a constant amount of extra space for variables like ans, i, and j. Therefore, the space complexity is O(1).

Code Examples (with slight improvements for readability):

The provided code in various languages is already quite efficient for this brute-force approach. However, here's a slightly refined Python version:

def findLUSlength(strs):
    def is_subsequence(s, t):
        i = 0
        for char in t:
            if i < len(s) and char == s[i]:
                i += 1
        return i == len(s)
 
    max_len = -1
    for i, s in enumerate(strs):
        is_unique = True
        for j, t in enumerate(strs):
            if i != j and is_subsequence(s, t):
                is_unique = False
                break
        if is_unique:
            max_len = max(max_len, len(s))
    return max_len
 

This version uses a more concise is_subsequence function and slightly clearer variable names. The other language examples provided are equally valid and efficient for this approach.