To solve this problem, we need to find the length of the longest uncommon subsequence among a list of strings. An uncommon subsequence is a string that is a subsequence of exactly one string in the list.
Approach
- Frequency Count: First, we count the frequency of each string. Strings that appear more than once cannot be uncommon subsequences because any subsequence of such a string would be a subsequence of all its occurrences.
- Filter Unique Strings: We filter out strings that appear more than once, as they cannot contribute to the answer.
- Sort Unique Strings: We sort the unique strings in descending order of their length. This allows us to check longer strings first, so we can return the result as soon as we find a valid uncommon subsequence.
- Check Validity: For each unique string, check if it is a subsequence of any other string in the original list. If it is not, then it is an uncommon subsequence, and we return its length. If no such string exists, return -1.
Solution Code
from collections import Counter
def longest_uncommon_subsequence_ii(strs):
def is_subsequence(t, s):
if len(t) > len(s):
return False
i = j = 0
while i < len(t) and j < len(s):
if t[i] == s[j]:
i += 1
j += 1
return i == len(t)
freq = Counter(strs)
unique_strings = [s for s in strs if freq[s] == 1]
# Sort by descending length, then lex order (optional)
unique_strings.sort(key=lambda x: (-len(x), x))
for t in unique_strings:
is_uncommon = True
for s in strs:
if s == t:
continue
if is_subsequence(t, s):
is_uncommon = False
break
if is_uncommon:
return len(t)
return -1
Explanation
- Frequency Count: Using
Counterfrom thecollectionsmodule, we count how many times each string appears in the input list. - Filter Unique Strings: We keep only those strings that appear exactly once, as duplicates cannot be uncommon.
- Sorting: By sorting the unique strings in descending order of length, we prioritize checking longer strings first. This ensures that the first valid string we find is the longest possible.
- Subsequence Check: For each unique string, we check if it is a subsequence of any other string in the original list. If it is not, it is an uncommon subsequence, so we return its length immediately. If no valid string is found, return -1.
This approach efficiently narrows down the candidates for the longest uncommon subsequence and checks their validity, ensuring optimal performance given the problem constraints. The time complexity is manageable due to the small input size (up to 50 strings, each up to 100 characters).
Example Walkthrough
-
Input: ["aba", "cdc", "eae"]
All strings are unique. The longest string is "aba" (length 3). It is not a subsequence of the other two strings, so the answer is 3. -
Input: ["aaa", "aaa", "aa"]
The string "aa" is unique but is a subsequence of "aaa" (which appears twice). So no valid uncommon subsequence exists, return -1. -
Input: ["a", "b", "c"]
All strings are unique and none are subsequences of each other. The answer is 1.
This solution handles all these cases correctly.


作者声明:本文包含人工智能生成内容。