Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k 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 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...