748. Shortest Completing Word

https://leetcode.com/problems/shortest-completing-word/description/

class Solution {
public:
    string shortestCompletingWord(string licensePlate, vector<string>& words) {
        int cnt = 0;
        vector<int> dict(26,0);
        for (auto c : licensePlate) {
            if (isupper(c)) { cnt++; dict[c-'A']++; }
            if (islower(c)) { cnt++; dict[c-'a']++; }
        }
        
        string res;
        for (int i = 0; i < words.size(); i++) {
            const auto& w = words[i];
            
            vector<int> dictTemp = dict;
            int cntTemp = cnt;
            for (auto c : w) {
                if (dictTemp[c-'a'] > 0) {
                    dictTemp[c-'a']--;
                    cntTemp--;
                }
            }
            if (cntTemp == 0) {
                if (res.length() == 0 || res.length() > w.length())
                    res = w;
            }
        }
        return res;
    }
};

  

猜你喜欢

转载自www.cnblogs.com/JTechRoad/p/8988882.html