Leetcode地址:522. 最长特殊序列 II - 力扣(LeetCode)
1.非连续子序列判断:
时间复杂度:O(n)
空间复杂度:O(1)
def isSubStr(s,t):
ps,pt=0,0
while ps<len(s) and pt<len(t):
if s[ps]==t[pt]:
ps+=1
pt+=1
return ps==len(s)
2.双层循环寻找是否存在一个元素不是其他所有元素的子序列,记录这样的序列的最大长度
代码如下:
class Solution:
def findLUSlength(self, strs: List[str]) -> int:
def isSubStr(s,t):
ps,pt=0,0
while ps<len(s) and pt<len(t):
if s[ps]==t[pt]:
ps+=1
pt+=1
return ps==len(s)
ans=-1
for i,s in enumerate(strs):
check=True
for j,t in enumerate(strs):
if i!=j and isSubStr(s,t):
check=False
break
if check:
ans=max(ans,len(s))
return ans
时间复杂度:O(l *n^2)
空间复杂度:O(1)