题目描述:

自己的提交:O(2**n∗n∗m),m 为字符串长度

class Solution:
def maxLength(self, arr: List[str]) -> int:
from collections import Counter
if not arr:return
res = set()
n = len(arr)
def helper(idx, tmp):
if len(tmp)>:
return
count = Counter(tmp)
if all(i== for i in count.values()):
res.add(tmp)
for i in range(idx, n):
helper(i+, tmp + arr[i])
helper(, "")
maxi =
# print(res)
for i in res:
maxi = max(maxi,len(i))
return maxi

另:动态规划

from collections import Counter
class Solution:
def sToNum(self, s):
ans =
for c in s:
ans |= <<(ord(c) - ord("a"))
return ans
def count(self, num):
ans =
while num:
num -= num&(-num)
ans +=
return ans
def maxLength(self, A):
A = [word for word in A if len(word) == len(set(word))]
sA = [self.sToNum(word) for word in A] aSet = set([])
for word in sA:
newS = set(aSet)
for s in aSet:
if not (s & word):
newS.add(s | word)
aSet = newS
# print (aSet)
return max(self.count(word) for word in aSet)

leetcode-160周赛-5240-串联字符串的最大长度的更多相关文章

  1. Leetcode 1239. 串联字符串的最大长度

    地址 https://leetcode-cn.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/s ...

  2. LeetCode1239串联字符串的最大长度

    题目 给定一个字符串数组 arr,字符串 s 是将 arr 某一子序列字符串连接所得的字符串,如果 s 中的每一个字符都只出现过一次,那么它就是一个可行解. 请返回所有可行解 s 中最长长度. 解题 ...

  3. C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

    剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...

  4. Leetcode:003 无重复字符串

    Leetcode:003 无重复字符串 关键点:给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度.示例 1:输入: "abcabcbb"输出: 3 解释: 因为无重复 ...

  5. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

  6. LeetCode 680. 验证回文字符串 Ⅱ(Valid Palindrome II) 1

    680. 验证回文字符串 Ⅱ 680. Valid Palindrome II 题目描述 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 每日一算法2019/5/4Day 1Le ...

  7. 【python】Leetcode每日一题-扰乱字符串

    [python]Leetcode每日一题-扰乱字符串 [题目描述] 使用下面描述的算法可以扰乱字符串 s 得到字符串 t : 如果字符串的长度为 1 ,算法停止 如果字符串的长度 > 1 ,执行 ...

  8. Leetcode 680.验证回文字符串

    验证回文字符串 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca&q ...

  9. 力扣(LeetCode)验证回文字符串II 个人题解

    给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca" 输出: ...

随机推荐

  1. 启动数据库报:ORA-03113

    1启动数据库报:ORA-03113 2.查看oracle运行日志 tail  -50 //home/app/oracle/diag/rdbms/orcl/orcl/trace/alert_orcl.l ...

  2. C++11新特性之 Move semantics(移动语义)

    https://blog.csdn.net/wangshubo1989/article/details/49748703 这篇讲到了vector的push_back的两种重载版本,左值版本和右值版本.

  3. 【unp】unix网络编程卷1-->环境搭建(ubuntu14.04)

    学习unp网络编程,树上的例子均存在#include "unp.h",故需要对环境进行配置. 1. 到资源页下载unpv13e 2. 解压并将unpv13e 移动到相应的文件夹下 ...

  4. I2C_24c02实验

    一.RCC初始化 /* Setup the microcontroller system. Initialize the Embedded Flash Interface, initialize th ...

  5. centos7.5部署ELk

    第1章 环境规划 1.1 ELK介绍       ELK是ElasticSerach.Logstash.Kibana三款产品名称的首字母集合,用于日志的搜集和搜索.      Elasticsearc ...

  6. mysql性能查看 命中率 慢查询

    网上有很多的文章教怎么配置MySQL服务器,但考虑到服务器硬件配置的不同,具体应用的差别,那些文章的做法只能作为初步设置参考,我们需要根据自己的情况进行配置优化,好的做法是MySQL服务器稳定运行了一 ...

  7. php错误提示

    1.配置php.ini打开php.ini,把display_errors = Off 改成 display_errors = On然后找到 error_reporting 最好设置成error_rep ...

  8. 【进阶技术】一篇文章搞掂:Spring高级编程

    本文篇幅较长,建议合理利用右上角目录进行查看(如果没有目录请刷新). 本文基于<Spring5高级编程>一书进行总结和扩展,大家也可以自行研读此书. 十一.任务调度 任务调度主要由三部分组 ...

  9. “void * __cdecl operator new(unsigned int)”(??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj)

    转自VC错误:http://www.vcerror.com/?p=1377 问题描述: 当 C 运行时 (CRT) 库和 Microsoft 基础类 (MFC) 库的链接顺序有误时,可能会出现以下 L ...

  10. oracle查询不显示小数点前的0

    1.问题起源       oracle 数据库字段值为小于1的小数时,使用char类型处理,会丢失小数点前面的0       例如0.35就变成了.35 2.解决办法:用to_char函数格式化数字显 ...