原题链接在这里: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;
}
}

LeetCode 1239. Maximum Length of a Concatenated String with Unique Characters的更多相关文章

  1. 【leetcode】1239. 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 ...

  2. [LeetCode] 718. Maximum Length of Repeated Subarray 最长的重复子数组

    Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...

  3. LN : leetcode 646 Maximum Length of Pair Chain

    lc 646 Maximum Length of Pair Chain 646 Maximum Length of Pair Chain You are given n pairs of number ...

  4. [LeetCode] Maximum Length of Repeated Subarray 最长的重复子数组

    Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...

  5. LeetCode Maximum Length of Pair Chain

    原题链接在这里:https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题目: You are given n  ...

  6. LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)

    718. 最长重复子数组 718. Maximum Length of Repeated Subarray 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的 ...

  7. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  8. [LeetCode]Maximum Length of Repeated Subarray

    Maximum Length of Repeated Subarray: Given two integer arrays A and B, return the maximum length of ...

  9. Maximum length of a table name in MySQL

    http://dev.mysql.com/doc/refman/5.7/en/identifiers.html The following table describes the maximum le ...

随机推荐

  1. WAV文件读取

    WAV是一种以RIFF为基础的无压缩音频编码格式,该格式以Header.Format Chunk及Data Chunk三部分构成. 本文简要解析了各部分的构成要素,概述了如何使用C++对文件头进行解析 ...

  2. python 2种创建多线程的方法

    多个线程是可以操作同一个全局变量的,因此,可以通过这种方式来判断所有线程的执行进度 # 第一种方法:将要执行的方法作为参数传给Thread的构造方法 import threading import t ...

  3. 基于hystrix的线程池隔离

    hystrix进行资源隔离,其实是提供了一个抽象,叫做command,就是说,你如果要把对某一个依赖服务的所有调用请求,全部隔离在同一份资源池内 对这个依赖服务的所有调用请求,全部走这个资源池内的资源 ...

  4. repodata创建本地YUM仓库

    参考一createrepo是linux下的创建仓库的软件包.create是创建的意思,repo是repository的缩写,是仓库的意思. yum(Yellow dog Updater,Modifie ...

  5. ELK +Nlog 分布式日志系统的搭建 For Windows

    前言 我们为啥需要全文搜索 首先,我们来列举一下关系型数据库中的几种模糊查询 MySql : 一般情况下LIKE 模糊查询  SELECT * FROM `LhzxUsers` WHERE UserN ...

  6. angular复习笔记2-架构总览

    angular架构总览 一个完整的Angular应用主要由6个重要部分构成,分别是:组件.模板.指令.服务.依赖注入和路由.这些组成部分各司其职,而又紧密协作,它们的关系如图所示. 与用户直接交互的是 ...

  7. 自己用JQueryUI封装了几个系统常用对话框

    /* * @功能描述:各种系统消息框 * @前置插件:JQueryUI * @开 发 者:魏巍 * @开发日期:2015-04-15 * @version 1.0 */ var SF = {}; SF ...

  8. 换个语言学一下 Golang (6)——数组,切片和字典

    在上面的章节里面,我们讲过Go内置的基本数据类型.现在我们来看一下Go内置的高级数据类型,数组,切片和字典. 数组(Array) 数组是一个具有相同数据类型的元素组成的固定长度的有序集合.比如下面的例 ...

  9. CSS样式表及选择器相关内容(一)

    CSS(Cascading Style Sheets)层叠样式表1.CSS级联-CSS层叠:规定在哪个HTML中使用哪个样式. body{ font-size:16px; //body默认字体大小 } ...

  10. Nginx配置SSL实现HTTPS访问

    nginx配置文件如下: server { listen 443 ssl; server_name www.domain.com; root /www/web; index index.html in ...