aboutsummaryrefslogtreecommitdiffstats
path: root/1239/main.py
blob: 0ef5ef18790625f6ac61592352699933f2e915d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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()