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

  1. Input: arr = ["un","iq","ue"]
  2. Output: 4
  3. Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
  4. Maximum length is 4.

Example 2:

  1. Input: arr = ["cha","r","act","ers"]
  2. Output: 6
  3. Explanation: Possible solutions are "chaers" and "acters".

Example 3:

  1. Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
  2. 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:

  1. class Solution {
  2. public int maxLength(List<String> arr) {
  3. int res = 0;
  4. List<Integer> candidates = new ArrayList<>();
  5. candidates.add(0);
  6.  
  7. for(String s : arr){
  8. int dup = 0;
  9. int sBit = 0;
  10. for(char c : s.toCharArray()){
  11. dup |= sBit & 1<<(c-'a');
  12. sBit |= 1<<(c-'a');
  13. }
  14.  
  15. if(dup > 0){
  16. continue;
  17. }
  18.  
  19. for(int i = 0; i<candidates.size(); i++){
  20. if((candidates.get(i) & sBit) > 0){
  21. continue;
  22. }
  23.  
  24. candidates.add(candidates.get(i) | sBit);
  25. res = Math.max(res, Integer.bitCount(candidates.get(i) | sBit));
  26. }
  27. }
  28.  
  29. return res;
  30. }
  31. }

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. spark streaming与storm比较

  2. linux查看当前路径命令 pwd

    pwd命令能够显示当前所处的路径. 这个命令比较简单,如果有时在操作过程中忘记了当前的路径,则可以通过此命令来查看路径,其执行方式为: # pwd /home/your_username 第一行为运行 ...

  3. Redis AOF持久化(二)

    1.AOF持久化的配置 AOF持久化,默认是关闭的,默认是打开RDB持久化 appendonly yes,可以打开AOF持久化机制,在生产环境里面,一般来说AOF都是要打开的,除非你说随便丢个几分钟的 ...

  4. springboot使用HttpSessionListener 监听器统计当前在线人数

    概括: request.getSession(true):若存在会话则返回该会话,否则新建一个会话. request.getSession(false):若存在会话则返回该会话,否则返回NULL ht ...

  5. TCP,UDP 通讯的helper类

    使用Tcp通讯,首先要启动tcp服务端监听客户端,客户端发送消息,服务端收到消息 1.服务端代码如下 public class TcpServerTest { public async Task Be ...

  6. left join 左边有数据,右边无数据

     参考了链接: https://blog.csdn.net/chenjianandiyi/article/details/52402011   主要是and和where的区别:   原Sql: Con ...

  7. Net Core 自定义 Middleware 加密解密

    前言:第一次写文章,有问题请轻喷 当前使用 Net Core 版本 2.1.3 我们经常在开发中需要把实体的主键 Id 传输到前端,但是在Get的时候又不想让前端能看到明文,我们通常会加密这些数据,所 ...

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

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

  9. 如何使用Git 优雅的版本回退呢?

    在版本迭代开发过程中,相信很多人都会有过错误提交的时候(至少良许有过几次这样的体验).这种情况下,菜鸟程序员可能就会虎驱一震,紧张得不知所措.而资深程序员就会微微一笑,摸一摸锃亮的脑门,然后默默的进行 ...

  10. margin 外边距合并问题

    一.兄弟元素的外边距合并 效果图如下:(二者之间的间距为100px,不是150px) 二.嵌套元素的外边距合并 对于两个嵌套关系的元素,如果父元素中没有内容或者内容在子元素的后面并且没有上内边距及边框 ...