522. 最长特殊序列 II

exiaohu 于 2022-06-27 发布

题目链接:522. 最长特殊序列 II

模拟。

from collections import Counter

class Solution:
    def findLUSlength(self, strs: List[str]) -> int:
        counter = Counter(strs)
        sorts = sorted(counter.keys(), key=len, reverse=True)
        for i, item in enumerate(sorts):
            if counter[item] > 1:
                continue

            for pre in sorts[:i]:
                if self.is_sub(pre, item):
                    break
            else:
                return len(item)

        return -1

    def is_sub(self, father: str, son: str):
        i, j = 0, 0
        while i < len(father) and j < len(son):
            if father[i] == son[j]:
                j += 1
            i += 1
        if j < len(son):
            return False
        return True