Accepted 20ms very simple C++


  • -1
    M
    string minWindow(string s, string t) {
        int n = (int)s.size(), m = (int)t.size();
        if (n == 0 || m == 0 || m > n) return "";
        int rc[256], cc[256];
        fill_n(rc, 256, 0);
        fill_n(cc, 256, 0);
        for (char c : t)
            rc[c+128]++;
        int k = 0;
        auto l = s.begin(), h = l, lm = l, hm = s.end();
        while (h < s.end()) {
            if (rc[*h+128]>0) {
                if (cc[*h+128]<rc[*h+128])
                    k++;
                cc[*h+128]++;
            }
            h++;
            if (k < m) continue;
            while (rc[*l+128]==0 || cc[*l+128]>rc[*l+128]) {
                if (rc[*l+128]>0 && cc[*l+128]>rc[*l+128])
                    cc[*l+128]--;
                l++;
            }
            if (distance(l, h)<distance(lm,hm))
                lm = l, hm = h;
        } 
        if (k < m) return "";
        return string(lm, hm);
    }

  • 0
    M

    Where's distance() defined?


  • 0
    M

    "distance" defined in c++ standard library.


Log in to reply
 

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