diff options
author | terminaldweller <devi@terminaldweller.com> | 2024-01-23 02:05:33 +0000 |
---|---|---|
committer | terminaldweller <devi@terminaldweller.com> | 2024-01-23 02:05:33 +0000 |
commit | f91f937859b203488ef7813d3e66c1b408f07d8d (patch) | |
tree | 861b8806b3889588c6f9be9dc29dfb4ce42accd8 /1239/main.py | |
parent | 645 (diff) | |
download | leetcode-main.tar.gz leetcode-main.zip |
Diffstat (limited to '1239/main.py')
-rwxr-xr-x | 1239/main.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/1239/main.py b/1239/main.py new file mode 100755 index 0000000..0ef5ef1 --- /dev/null +++ b/1239/main.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python + + +import typing + + +class Solution: + def maxLength(self, arr: typing.List[str]) -> int: + result = [0] + self.dfs(arr, "", 0, result) + return result[0] + + def dfs(self, arr, path, idx, result): + if self.isUniqueChars(path): + result[0] = max(result[0], len(path)) + + if idx == len(arr) or not self.isUniqueChars(path): + return + + for i in range(idx, len(arr)): + self.dfs(arr, path + arr[i], i + 1, result) + + def isUniqueChars(self, s): + char_set = set() + for c in s: + if c in char_set: + return False + char_set.add(c) + return True + + +def main(): + solution = Solution() + print(solution.maxLength(["un", "iq", "ue"])) + print(solution.maxLength(["cha", "r", "act", "ers"])) + print(solution.maxLength(["abcdefghijklmnopqrstuvwxyz"])) + + +if __name__ == "__main__": + main() |