Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def maxLength(self, arr: List[str]) -> int:
- # Remove words with duplicate letters
- arr = [s for s in arr if len(s) == len(set(s))]
- def to_letters_bitset(s: str) -> int:
- ret = 0
- for c in s:
- ret |= 1 << (ord(c) - ord('a'))
- return ret
- letters = [to_letters_bitset(s) for s in arr]
- ans = 0
- for comb in range(1, 2 ** len(arr)):
- cur_letters = 0
- cur_len = 0
- for i in range(len(arr)):
- if ((2 ** i) & comb) == 0:
- continue
- if (cur_letters & letters[i]) != 0:
- break
- cur_letters |= letters[i]
- cur_len += len(arr[i])
- # We don't have to care if comb is valid as used_letters and
- # cur_len are only increased when we have a valid prefix of comb
- ans = max(ans, cur_len)
- return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement