Advertisement
kosievdmerwe

Untitled

Sep 22nd, 2021
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. class Solution:
  2.     def maxLength(self, arr: List[str]) -> int:
  3.         # Remove words with duplicate letters
  4.         arr = [s for s in arr if len(s) == len(set(s))]
  5.        
  6.         def to_letters_bitset(s: str) -> int:
  7.             ret = 0
  8.             for c in s:
  9.                 ret |= 1 << (ord(c) - ord('a'))
  10.             return ret
  11.         letters = [to_letters_bitset(s) for s in arr]
  12.        
  13.         ans = 0
  14.         for comb in range(1, 2 ** len(arr)):
  15.             cur_letters = 0
  16.             cur_len = 0
  17.             for i in range(len(arr)):
  18.                 if ((2 ** i) & comb) == 0:
  19.                     continue
  20.                 if (cur_letters & letters[i]) != 0:
  21.                     break
  22.                 cur_letters |= letters[i]
  23.                 cur_len += len(arr[i])
  24.             # We don't have to care if comb is valid as used_letters and
  25.             # cur_len are only increased when we have a valid prefix of comb
  26.             ans = max(ans, cur_len)
  27.         return ans
  28.  
  29.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement