leetcode - 1239. Maximum Length of a Concatenated String with Unique Characters

Description

You are given an array of strings arr. A string s is formed by the concatenation of a subsequence of arr that has unique characters.

Return the maximum possible length of s.

A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.

Example 1:

复制代码
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All the valid concatenations are:
- ""
- "un"
- "iq"
- "ue"
- "uniq" ("un" + "iq")
- "ique" ("iq" + "ue")
Maximum length is 4.

Example 2:

复制代码
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible longest valid concatenations are "chaers" ("cha" + "ers") and "acters" ("act" + "ers").

Example 3:

复制代码
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Explanation: The only string in arr has all 26 characters.

Constraints:

复制代码
1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] contains only lowercase English letters.

Solution

Recursive

Recursive, for each string, use it or don't use it. We could only use it when the characters don't appear before.

Time complexity: o ( 2 n ) o(2^n) o(2n)

Space complexity: o ( n ) o(n) o(n)

DP

Use set in dp, every time check if current string could be added to the set.

Code

Recursive

python3 复制代码
class Solution:
    def maxLength(self, arr: List[str]) -> int:
        visited_char = {}
        def helper(index: int) -> int:
            if index >= len(arr):
                return 0
            res = 0
            could_use = True
            for each_c in arr[index]:
                if visited_char.get(each_c, 0) > 0:
                    could_use = False
                visited_char[each_c] = visited_char.get(each_c, 0) + 1
            if could_use:
                res = max(res, len(arr[index]) + helper(index + 1))
            # remove from visited_set, don't use
            for each_c in arr[index]:
                visited_char[each_c] -= 1
            res = max(res, helper(index + 1))
            return res
        return helper(0)

DP

python3 复制代码
class Solution:
    def maxLength(self, arr: List[str]) -> int:
        dp = [set()]
        for each_string in arr:
            string_set = set(each_string)
            if len(string_set) < len(each_string):
                continue
            prev_dp = dp[:]
            for prev_set in prev_dp:
                if string_set & prev_set:
                    continue
                else:
                    dp.append(string_set | prev_set)
        return max(len(each_set) for each_set in dp)
相关推荐
少许极端12 小时前
算法奇妙屋(四十六)-二分答案
算法·二分答案
漂流瓶jz13 小时前
UVA-120 煎饼 题解答案代码 算法竞赛入门经典第二版
数据结构·c++·算法·排序·aoapc·算法竞赛入门经典·uva
paeamecium13 小时前
【PAT甲级真题】- Stack (30)
数据结构·算法·pat考试·pat
黎阳之光13 小时前
黎阳之光核工厂202应急管控平台|全域实景孪生,筑牢核安全最后一道防线
大数据·人工智能·算法·安全·数字孪生
优秀13513 小时前
计算机基础面试重点知识
网络·面试·职场和发展
莫等闲-13 小时前
代码随想录一刷记录Day31——leetcode56. 合并区间 738.单调递增的数字
数据结构·c++·算法·leetcode
克里普crirp13 小时前
短波通信的可用频率计算方法
人工智能·算法·机器学习
剑挑星河月13 小时前
45.跳跃游戏Ⅱ
数据结构·算法·leetcode
hqyjzsb14 小时前
AI培训课程怎么设计才有效?
人工智能·职场和发展·aigc·产品经理·学习方法·业界资讯·设计语言
MegaDataFlowers14 小时前
1.两数之和
算法