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
.
"abc"
is a subsequence of "aebdc"
because you can delete the underlined characters in "a<u>e</u>b<u>d</u>c"
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
Example 3:
Input: strs = ["abcd","abc","ab"]
Output: 4
Constraints:
2 <= strs.length <= 50
1 <= strs[i].length <= 10
strs[i]
consists of lowercase English letters.def is_subsequence(s, t):
"""Checks if string s is a subsequence of string t."""
i = 0 # Pointer for s
j = 0 # Pointer for t
while i < len(s) and j < len(t):
if s[i] == t[j]:
i += 1
j += 1
return i == len(s)
def longest_uncommon_subsequence(strs):
"""Finds the length of the longest uncommon subsequence among strings."""
n = len(strs)
max_len = -1
for i in range(n):
is_uncommon = True
for j in range(n):
if i != j and is_subsequence(strs[i], strs[j]):
is_uncommon = False
break
if is_uncommon:
max_len = max(max_len, len(strs[i]))
return max_len
# Example Usage
strs1 = ["aba", "cdc", "eae"]
print(f"Longest uncommon subsequence length for {strs1}: {longest_uncommon_subsequence(strs1)}")
strs2 = ["aaa", "aaa", "aa"]
print(f"Longest uncommon subsequence length for {strs2}: {longest_uncommon_subsequence(strs2)}")
strs3 = ["abcd", "abcd", "abcde"]
print(f"Longest uncommon subsequence length for {strs3}: {longest_uncommon_subsequence(strs3)}")
strs4 = ["a", "b", "c"]
print(f"Longest uncommon subsequence length for {strs4}: {longest_uncommon_subsequence(strs4)}")
The naive approach involves checking every string against all other strings to see if it's a subsequence of any of them. If a string is not a subsequence of any other string, it's a candidate for the longest uncommon subsequence. We keep track of the maximum length of such strings.
The provided code directly implements the optimal approach within the given constraints. Since the constraints limit the number of strings to 50 and the maximum length of each string to 10, the time complexity is acceptable. The algorithm iterates through each string and checks if it's a subsequence of any other string. If it's not, the length of the string is compared with the current maximum length to update it.
strs
array once, which contributes a factor of n, where n is the number of strings.is_subsequence
Function: The is_subsequence
function compares two strings. In the worst case, it might iterate through both strings completely, contributing a factor of m, where m is the maximum length of the strings.is_subsequence
function. These variables use a constant amount of space.strs
is not considered as part of the algorithm's space complexity.is_subsequence
function uses a constant amount of space.strs
is empty, the code will not execute the loops, and the initial value of max_len
(-1) will be returned. Although the problem statement says 2 <= strs.length <= 50
, it's good to be aware of this case.