Leetcode 1239. 串联字符串的最大长度

地址 https://leetcode-cn.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/submissions/

给定一个字符串数组 arr,字符串 s 是将 arr 某一子序列字符串连接所得的字符串,如果 s 中的每一个字符都只出现过一次,那么它就是一个可行解。

请返回所有可行解 s 中最长长度。

示例 1:

输入:arr = ["un","iq","ue"]
输出:4
解释:所有可能的串联组合是 "","un","iq","ue","uniq""ique",最大长度为 4。
示例 2:

输入:arr = ["cha","r","act","ers"]
输出:6
解释:可能的解答有 "chaers""acters"。
示例 3:

输入:arr = ["abcdefghijklmnopqrstuvwxyz"]
输出:26
 

提示:

1 <= arr.length <= 16
1 <= arr[i].length <= 26
arr[i] 中只含有小写英文字母

题解:

1 首先考虑DFS 遍历各种组合 

2 考虑使用bit代表各个字符出现与否

  这里为了代码写的方便 使用了一个26长度的数组 记录字符是否出现与否

代码

class Solution {
public:
    vector<int>  currenthash;
int currentLen;
vector<vector<int>> hashVec;
int ret = -9999;

bool CheckHash(vector<int>& hash1, vector<int>& hash2) {
    for (int i = 0; i < hash1.size(); i++) {
        if (hash1[i] + hash2[i] > 1)
            return false;
    }

    return true;
}

void startTry(int idx,int currentLen, vector<string>& arr)
{
    if (idx == arr.size()) {
        ret = max(ret, currentLen);
        return;
    }

    if (CheckHash(currenthash, hashVec[idx])) {
        currentLen += arr[idx].size();
        for (int i = 0; i < currenthash.size(); i++) {
            currenthash[i] += hashVec[idx][i];
        }
        startTry(idx + 1, currentLen,arr);
        for (int i = 0; i < currenthash.size(); i++) {
            currenthash[i] -= hashVec[idx][i];
        }
        currentLen -= arr[idx].size();
    }
    startTry(idx + 1, currentLen, arr);

}

int maxLength(vector<string>& arr) {
    currenthash = vector<int>(26, 0);
    currentLen = 0;
    hashVec = vector<vector<int>>(arr.size(), vector<int>(26, 0));

    for (int i = 0; i < arr.size(); i++) {
        for (int j = 0; j < arr[i].size(); j++) {
            int idx = arr[i][j] - 'a';
            hashVec[i][idx]++;
            //优化
        }
    }

    currenthash = vector<int>(26, 0);
    currentLen = 0;
    startTry(0, 0,arr);
    return ret;
}

};

猜你喜欢

转载自www.cnblogs.com/itdef/p/11762027.html