原题链接在这里:https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/

题目:

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.

题解:

In the arr, s could contain duplicate character. These strings with duplicate characters can't be used.

Have a list to maintain all previous bit masks. When checking a new string s, check all previous bit masks, if there is not intersection, then s could be used.

Update the maximum result and add the new bitmast into list.

Time Complexity: expontential.

Space: expontential.

AC Java:

 class Solution {
public int maxLength(List<String> arr) {
int res = 0;
List<Integer> candidates = new ArrayList<>();
candidates.add(0); for(String s : arr){
int dup = 0;
int sBit = 0;
for(char c : s.toCharArray()){
dup |= sBit & 1<<(c-'a');
sBit |= 1<<(c-'a');
} if(dup > 0){
continue;
} for(int i = 0; i<candidates.size(); i++){
if((candidates.get(i) & sBit) > 0){
continue;
} candidates.add(candidates.get(i) | sBit);
res = Math.max(res, Integer.bitCount(candidates.get(i) | sBit));
}
} return res;
}
}

最新文章

  1. Mac &gt; MacBook Pro的移动硬盘方案
  2. 微信小程序 wx.uploadFile在安卓手机上面the same task is working问题解决
  3. 编译预处理命令--define和ifdef的使用
  4. 移动web开发--meta 之 viewport
  5. [javascript]event属性
  6. Endian.BIG_ENDIAN和Endian.LITTLE_ENDIAN(http://smartblack.iteye.com/blog/1129097)
  7. oracle日期函数集锦
  8. CocoaPods 建立私有仓库
  9. MongoDB分片原理篇
  10. 阿里云服务器部署php的laravel项目,在阿里云买ECS 搭建 Linux+Nginx+Mysql+PHP环境的
  11. push_back和emplace_back的区别
  12. EBS DBA指南笔记(三)
  13. 专治编译器编辑器vscode中文乱码输出 win10 配置系统默认utf-8编码
  14. Linux简易APR内存池学习笔记(带源码和实例)
  15. 《C#并发编程经典实例》学习笔记-进程(process)和线程(thread)
  16. 使用VSTS的Git进行版本控制(四)——在Visual Studio中管理分支
  17. Spark基础脚本入门实践3:Pair RDD开发
  18. EF访问数据库报“ExecuteReader 要求已打开且可用的 Connection。连接的当前状态为已关闭。”错误
  19. PGIS下载离线地图 SQLite+WPF
  20. Caffe 深度学习框架上手教程

热门文章

  1. JAVA 8 的新特性
  2. PAT甲级1006水题飘过
  3. Python 入门(1):hello world 到流程控制
  4. jwt的思考
  5. mysql执行出错:Table &#39;k_user&#39; is read only
  6. Asp.net MVC 之ActionResult
  7. Java调用Http/Https接口(2)--HttpURLConnection/HttpsURLConnection调用Http/Https接口
  8. Lipo移除ORC架构
  9. 微信小程序项目开发实战:用WePY、mpvue、Taro打造高效的小程序》(笔记4)支持React.js语法的Taro框架
  10. KVM on CubieTruck 原理以及网络性能相关思考