返回介绍

solution / 1200-1299 / 1239.Maximum Length of a Concatenated String with Unique Characters / README_EN

发布于 2024-06-17 01:03:21 字数 5389 浏览 0 评论 0 收藏 0

1239. Maximum Length of a Concatenated String with Unique Characters

中文文档

Description

You are given an array of strings arr. A string s is formed by the concatenation of a subsequence of arr that has unique characters.

Return _the maximum possible length_ of s.

A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.

 

Example 1:

Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All the valid concatenations are:
- ""
- "un"
- "iq"
- "ue"
- "uniq" ("un" + "iq")
- "ique" ("iq" + "ue")
Maximum length is 4.

Example 2:

Input: arr = ["cha","r","act","ers"]
Output: 6
Explanation: Possible longest valid concatenations are "chaers" ("cha" + "ers") and "acters" ("act" + "ers").

Example 3:

Input: arr = ["abcdefghijklmnopqrstuvwxyz"]
Output: 26
Explanation: The only string in arr has all 26 characters.

 

Constraints:

  • 1 <= arr.length <= 16
  • 1 <= arr[i].length <= 26
  • arr[i] contains only lowercase English letters.

Solutions

Solution 1: Bit Manipulation + State Compression

State compression is used, with a 32-bit number recording the occurrence of letters, and masks storing the strings enumerated before.

The time complexity is $O(2^n + L)$, and the space complexity is $O(2^n)$. Where $n$ and $L$ are the length of the string array and the sum of the lengths of the strings in the array, respectively.

class Solution:
  def maxLength(self, arr: List[str]) -> int:
    ans = 0
    masks = [0]
    for s in arr:
      mask = 0
      for c in s:
        i = ord(c) - ord('a')
        if mask >> i & 1:
          mask = 0
          break
        mask |= 1 << i
      if mask == 0:
        continue
      for m in masks:
        if m & mask == 0:
          masks.append(m | mask)
          ans = max(ans, (m | mask).bit_count())
    return ans
class Solution {
  public int maxLength(List<String> arr) {
    int ans = 0;
    List<Integer> masks = new ArrayList<>();
    masks.add(0);
    for (var s : arr) {
      int mask = 0;
      for (int i = 0; i < s.length(); ++i) {
        int j = s.charAt(i) - 'a';
        if (((mask >> j) & 1) == 1) {
          mask = 0;
          break;
        }
        mask |= 1 << j;
      }
      if (mask == 0) {
        continue;
      }
      int n = masks.size();
      for (int i = 0; i < n; ++i) {
        int m = masks.get(i);
        if ((m & mask) == 0) {
          masks.add(m | mask);
          ans = Math.max(ans, Integer.bitCount(m | mask));
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  int maxLength(vector<string>& arr) {
    int ans = 0;
    vector<int> masks = {0};
    for (auto& s : arr) {
      int mask = 0;
      for (auto& c : s) {
        int i = c - 'a';
        if (mask >> i & 1) {
          mask = 0;
          break;
        }
        mask |= 1 << i;
      }
      if (mask == 0) {
        continue;
      }
      int n = masks.size();
      for (int i = 0; i < n; ++i) {
        int m = masks[i];
        if ((m & mask) == 0) {
          masks.push_back(m | mask);
          ans = max(ans, __builtin_popcount(m | mask));
        }
      }
    }
    return ans;
  }
};
func maxLength(arr []string) (ans int) {
  masks := []int{0}
  for _, s := range arr {
    mask := 0
    for _, c := range s {
      i := int(c - 'a')
      if mask>>i&1 == 1 {
        mask = 0
        break
      }
      mask |= 1 << i
    }
    if mask == 0 {
      continue
    }
    n := len(masks)
    for _, m := range masks[:n] {
      if m&mask == 0 {
        masks = append(masks, m|mask)
        ans = max(ans, bits.OnesCount(uint(m|mask)))
      }
    }
  }
  return
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文