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 分钟前
查找-从二分查找到二叉排序树
数据结构·c++·算法
程序猿追42 分钟前
画个饼,给数据点颜色看看——在 HarmonyOS 模拟器上手搓一个饼图/环形图组件
深度学习·算法·harmonyos
秦明月131 小时前
EPLAN部件库整理之维护篇----部件库整理收尾:做好日常维护,再也不用反复重做
经验分享·其他·职场和发展·学习方法·设计规范
net3m331 小时前
mymalloc函数里增加memset来初始化数据 全为0,能解决一些奇怪的问题,
算法
计算机安禾1 小时前
【算法分析与设计】第43篇:空间复杂度类与Savitch定理
java·服务器·网络·数据库·算法
圣保罗的大教堂1 小时前
leetcode 3751. 范围内总波动值 I 中等
leetcode
JAVA社区1 小时前
Java高级全套教程(十四)—— SpringData超详细实战详解
java·开发语言·spring cloud·面试·职场和发展
8Qi81 小时前
LeetCode 416:分割等和子集 —— (0-1背包)
java·算法·leetcode·动态规划·背包问题·01背包
智者知已应修善业2 小时前
【51单片机数码管驱动2位显示0-99按键3短按+1长按+10按键4短按-1长按清零,按键不影响数码管显示】2023-8-16
c++·经验分享·笔记·算法·51单片机
rou2 小时前
Stream Response
算法