给定字符串列表 strs ,返回其中 最长的特殊序列 。如果最长特殊序列不存在,返回 -1 。
特殊序列 定义如下:该序列为某字符串 独有的子序列(即不能是其他字符串的子序列)。
s 的 子序列可以通过删去字符串 s 中的某些字符实现。
示例 1:
输入: strs = [“aba”,“cdc”,“eae”]
输出: 3
示例 2:
输入: strs = [“aaa”,“aaa”,“aa”]
输出: -1
提示:
2 <= strs.length <= 50
1 <= strs[i].length <= 10
strs[i] 只包含小写英文字母
class Solution:
def findLUSlength(self, strs: List[str]) -> int:
# 判断是否为子序列
def isson(str1, str2):
i,j = 0, 0
while i < len(str1) and j < len(str2):
if str1[i] == str2[j]:
i += 1
j += 1
else:
j += 1
return i == len(str1)
ans = -1
for i in range(len(strs)):
judge = True
for j in range(len(strs)):
if i == j:
continue
if isson(strs[i], strs[j]):
judge = False
break
# 更新结果的最大值
if judge:
ans = max(ans, len(strs[i]))
return ans