Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

time complexity - Leetcode - Maximum Length of a Concatenated String with Unique Characters

Leetcode question 1239: Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have unique characters. Return the maximum possible length of s.

The following is a solution:

class Solution(object):
    
    def __init__(self):
        
        self.result = 0
    
    def checkDuplicate(self, s):
        
        return len(s) != len(set(s))
    
    def helper(self, arr, index, curr):
    
        self.result = max(self.result, len(curr))

        for i in range(index, len(arr)):
            
            temp = curr + arr[i]
            
            if not self.checkDuplicate(temp):
                self.helper(arr, i, temp)
    
    
    def maxLength(self, arr):
        """
        :type arr: List[str]
        :rtype: int
        """
        
        index = 0
        curr = ''
        
        self.helper(arr, index, curr)
        
        return self.result

My question is related to the time complexity of this solution. Will it be O(n^2) ?

question from:https://stackoverflow.com/questions/65856560/leetcode-maximum-length-of-a-concatenated-string-with-unique-characters

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...