Longest Uncommon Subsequence II

Problem Id: 522 Difficulty: Medium


Intuition

Solution


class Solution:
    def findLUSlength(self, strs: List[str]) -> int:
        strs.sort(key=len, reverse=True)
        for i in range(len(strs)):
            flag = True
            for j in range(len(strs)):
                if i == j:
                    continue
                if self.subsequence(strs[j], strs[i]):
                    flag = False
                    break
            if flag:
                return len(strs[i])
        return -1

    def subsequence(self, s1, s2):
        i = j = 0
        while i < len(s1) and j < len(s2):
            if s1[i] == s2[j]:
                i += 1
                j += 1
            else:
                i += 1
        return j == len(s2)