Maximum Length of a Concatenated String with Unique Characters (Leetcode 1239)

Problem Link: https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/

class Solution:
    def maxLength(self, arr: List[str]) -> int:
        
        ans=0
        n=len(arr)
        for combination in range(2**n):
            
            seen=set()
            isValidString=True
            strlength=0
            
            for i in range(n):
                if((combination>>i)&1==1):
                    
                    for ch in arr[i]:
                        if(ch in seen):
                            isValidString=False
                            break
                        else:
                            strlength+=1
                            seen.add(ch)
            if(isValidString==True):
                if(strlength>ans):
                    ans=strlength
        return ans

Last updated