From f91f937859b203488ef7813d3e66c1b408f07d8d Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Mon, 22 Jan 2024 21:05:33 -0500 Subject: 1239 --- 1239/main.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100755 1239/main.py (limited to '1239') 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() -- cgit v1.2.3