题目如下:

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.

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: 26

Constraints:

  • 1 <= arr.length <= 16
  • 1 <= arr[i].length <= 26
  • arr[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

最新文章

  1. 编译器开发系列--Ocelot语言7.中间代码
  2. UML简单例子
  3. UISegmentedControl 的使用
  4. 图片上传功能&lt;转&gt;http://blog.csdn.net/u011159417/article/details/50126023
  5. geoip2 domain
  6. CentOS7安装memcached
  7. Autoprefixer处理CSS3属性前缀
  8. HDU 4512 吉哥系列故事——完美队形(LCIS)
  9. CentOS6.5 一键部署运行环境shell脚本
  10. HDU 1253 胜利大逃亡(三维BFS)
  11. oracle 分区和分区索引
  12. Struts2+JQuery发送Ajax请求
  13. POJ1719- Shooting Contest(二分图最大匹配)
  14. 详解Objective-C的meta-class 分类: ios相关 ios技术 2015-03-07 15:41 51人阅读 评论(0) 收藏
  15. Android 几种网络请求的区别与联系
  16. go标准库的学习-errors
  17. Jmeter中使用外部的java文件
  18. MYSQL语句:创建、授权、查询、修改、统计分析等 一 用户的创建、权限设置、删除等
  19. SVN无法Cleanup
  20. Codeforces Round #345 (Div. 2) B. Beautiful Paintings 暴力

热门文章

  1. Win10黑色白色主题切换
  2. windows 环境安装K8s
  3. java学习-2
  4. 1. centos7 的安装
  5. 解决ubuntu命令行中文乱码
  6. 什么是云数据库 HBase 版
  7. 2019.07.05 纪中_B
  8. 2019年8月23日 星期五(韩天峰的swoole)
  9. python之BeautifulSoup4
  10. Python两个内置函数locals 和globals