【leetcode】1239. Maximum Length of a Concatenated String with Unique Characters
题目如下:
Given an array of strings
arr. Stringsis a concatenation of a sub-sequence ofarrwhich have unique characters.Return the maximum possible length of
s.Example 1:
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.Example 2:
Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible solutions are "chaers" and "acters".Example 3:
Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26Constraints:
1 <= arr.length <= 161 <= arr[i].length <= 26arr[i]contains only lower case English letters.
解题思路:首先过滤掉arr中有重复字符的元素,接下来就可以采用BFS的方法进行计算了。怎么判断两个字符串是否有相同字符呢?我的方法是把字符串转成二进制的方式,a对应2^0,c对应2^2,z对应2^25。这样的话,每一个字符串都可以计算出一个特征值,判断两个字符串是否有相同字符,只有判断两个特征值与操作的结果是否为0即可。
代码如下:
class Solution(object):
def maxLength(self, arr):
"""
:type arr: List[str]
:rtype: int
"""
arr_val = []
for i in arr:
val = 0
if len(set(list(i))) != len(i):continue
for j in i:
val |= 2**(ord(j) - ord('a'))
arr_val.append(val)
#print arr_val
queue = []
if len(arr_val) == 0:return 0
for (inx,val) in enumerate(arr_val):
queue.append((inx,val))
res = 0
while len(queue) > 0:
inx,val = queue.pop(0)
end = True
for i in range(inx+1,len(arr_val)):
if val & arr_val[i] != 0:continue
queue.append((i,val | arr_val[i]))
end = False
if end :
res = max(res,bin(val).count(''))
return res
【leetcode】1239. Maximum Length of a Concatenated String with Unique Characters的更多相关文章
- LeetCode 1239. Maximum Length of a Concatenated String with Unique Characters
原题链接在这里:https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 【LeetCode】646. Maximum Length of Pair Chain 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【Leetcode】164. Maximum Gap 【基数排序】
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 【LeetCode】325. Maximum Size Subarray Sum Equals k 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 prefix Sum 日期 题目地址:https:// ...
- 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
随机推荐
- Django book 2.0 的中文翻译
传送门大法好: http://djangobook.py3k.cn/2.0/
- python selenium 实战涉及很多知识点
1.iframe的切入和切出 #切入 driver.switch_to.frame(driver.find_element_by_id('iFrame_1')) # 切换出来 driver.switc ...
- NOIp2017D1T2 时间复杂度【模拟】
说一说 题目分析请从目录空降... 没想到模拟题还会卡这么久...菜得真实... 这是一个励志的故事:从$0pts->9pts->18pts->27pts->36tps-> ...
- java中怎么调用python 脚本
调用方法: import java.io.BufferedReader; import java.io.InputStreamReader; public class PythonInvoke { p ...
- luoguP3390(矩阵快速幂模板题)
链接:https://www.luogu.org/problemnew/show/P3390 题意:矩阵快速幂模板题,思路和快速幂一致,只需提供矩阵的乘法即可. AC代码: #include<c ...
- Vue 中 $attrs 的使用
名词解释: $attrs--继承所有的父组件属性(除了prop传递的属性.class 和 style ) inheritAttrs:默认值true,继承所有的父组件属性(除props的特定绑定)作为普 ...
- 【pytorch】学习笔记(二)- Variable
[pytorch]学习笔记(二)- Variable 学习链接自莫烦python 什么是Variable Variable就好像一个篮子,里面装着鸡蛋(Torch 的 Tensor),里面的鸡蛋数不断 ...
- win10子系统Ubuntu重置
重置: 在win10命令行下执行: lxrun /uninstall /full 安装: win+R打开bash 执行命令: lxrun /install /y
- 一、程序安全-SQL注入漏洞
先新建MYDB.MDF,表MyUser: 测试页面: 一.利用报错获取信息 操作:按姓名精确查询,在输入框输入:小卫' and 1=db_name()/0 and '1'='1执行语句:select ...
- (转载)sublime3安装markdown插件
原文链接 http://www.jianshu.com/p/335b7d1be39e?utm_source=tuicool&utm_medium=referral 最近升级到了 Sublime ...