File size: 2,034 Bytes
3c88a0f
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "cp-10029", "name": "LeetCode 0522: Longest Uncommon Subsequence Ii"}
{"type": "message", "message": {"role": "system", "content": "You are an expert LeetCode problem solver. You solve problems efficiently with clean code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Longest Uncommon Subsequence II (Difficulty: Medium)\nTags: Array, Hash Table, Two Pointers, String, Sorting\n\nGiven an array of strings strs, return the length of the longest uncommon subsequence between them. If the longest uncommon subsequence does not exist, return -1.\nAn uncommon subsequence between an array of strings is a string that is a subsequence of one string but not the others.\nA subsequence of a string s is a string that can be obtained after deleting any number of characters from s.\n\nExample 1:\nInput: strs = [\"aba\",\"cdc\",\"eae\"]\nOutput: 3\n\nExample 2:\nInput: strs = [\"aaa\",\"aaa\",\"aa\"]\nOutput: -1\n\nConstraints:\n- 2 <= strs.length <= 50\n- 1 <= strs[i].length <= 10\n- strs[i] consists of lowercase English letters."}}
{"type": "message", "message": {"role": "assistant", "content": "# Time:  O(l * n^2)\n# Space: O(1)\n\nclass Solution(object):\n    def findLUSlength(self, strs):\n        \"\"\"\n        :type strs: List[str]\n        :rtype: int\n        \"\"\"\n        def isSubsequence(a, b):\n            i = 0\n            for j in range(len(b)):\n                if i >= len(a):\n                    break\n                if a[i] == b[j]:\n                    i += 1\n            return i == len(a)\n\n        strs.sort(key=len, reverse=True)\n        for i in range(len(strs)):\n            all_of = True\n            for j in range(len(strs)):\n                if len(strs[j]) < len(strs[i]):\n                    break\n                if i != j and isSubsequence(strs[i], strs[j]):\n                    all_of = False\n                    break\n            if all_of:\n                return len(strs[i])\n        return -1\n\n"}}