C++ Histogram solution


  • 0
    V
    class Solution {
        inline bool hasMet(vector<int>& a) {
            for (auto x : a)
                if (x > 0)
                    return false;
            return true;
        }
    public:
        string shortestCompletingWord(string licensePlate, vector<string>& words) {
            vector<int> h(26, 0);
            string ans = "";
            for (auto x : licensePlate)
                if (isalpha(x))
                    h[tolower(x) - 'a']++;
            
            for (auto w:words) {
                vector<int> f = h;
                for (auto x : w) {
                    if (isalpha(x)) {
                        f[tolower(x) - 'a']--;
                        if (hasMet(f) && (ans.length() > w.length() || ans.length() == 0)) {
                            ans = w;
                            break;
                        }       
                    }
                }
            }
            return ans;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.