521. Longest Uncommon Subsequence I & II [Easy & Medium]

521. Longest Uncommon Subsequence I

521. Longest Uncommon Subsequence I

这是什么题,excuse me ?

class Solution(object):
    def findLUSlength(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: int
        """
        if a == b:
            return -1
        else:
            return max(len(a),len(b))

522. Longest Uncommon Subsequence II

522. Longest Uncommon Subsequence II

刚开始没有看懂题,Subsequence是可以删掉中间的字符,所以要用两个指针,看看是不是能指向最后,优化就是把输入按照长度进行了一个排序,这样找到的第一个就是结果。

class Solution(object):
    def findLUSlength(self, strs):
        """
        :type strs: List[str]
        :rtype: int
        """
        strs.sort(key=lambda x:len(x), reverse=True)
        for i in range(len(strs)):
            count = len(strs) - 1
            for j in range(len(strs)):
                if i == j: continue
                if self.help(strs[i], strs[j])==False:
                    count -= 1
            if count == 0:
                return len(strs[I])
        return -1
    
    def help(self, s1, s2):
        if len(s1) > len(s2):
            return False
        if s1 == s2:
            return True
        i = j = 0
        while i < len(s1) and j < len(s2):
            if s1[i] == s2[j]:
                i += 1
                j += 1
            else:
                j += 1
        return i == len(s1)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容