Sliding Window
Nothing special, just optimize to not to create temporary strings.\n\nC++\ncpp\nstring shortestBeautifulSubstring(const string &s, int k) {\n    int ri = s.size
Nothing special, just optimize to not to create temporary strings.\n\n**C++**\n```cpp\nstring shortestBeautifulSubstring(const string &s, int k) {\n    int ri = s.size() - 1, rj = 0;\n    for (int i = 0, j = 0; i < s.size(); ++i) {\n        k -= s[i] == \'1\';\n        while (k < 0 || s[j] == \'0\')\n            k += s[j++] == \'1\';\n        if (k == 0 && ri - rj >= i - j)\n            if (ri - rj > i - j || (ri - rj == i - j && \n                s.compare(j, i - j + 1, s, rj, ri - rj + 1) < 0)) {\n                ri = i;\n                rj = j;\n            }\n    }\n    return k == 0 ? s.substr(rj, ri - rj + 1) : "";\n}\n```
['C']