📌 C++ || Sliding window || 8 lines || Faster  98.74% || Memory 100%
Intuition\n- We maintain a sliding window of substring with one_count equal to k\n- For valid, substring we keep the lexicographically smallest one\n\nCode\nc++
**Intuition**\n- We maintain a sliding window of substring with `one_count` equal to `k`\n- For valid, substring we keep the `lexicographically smallest` one\n\n**Code**\n```c++\nstring shortestBeautifulSubstring(string s, int k, string ans = "") {\n    for (auto i = 0, left = 0, one_count = 0; i < s.size(); i++) {\n        one_count += s[i] == \'1\';\n        if (one_count != k) continue;\n        \n        while (s[left] == \'0\') left++;\n        auto can_be = s.substr(left, i - left + 1);\n        auto is_ans = ans.empty()\n                    || can_be.size() < ans.size()\n                    || can_be.size() == ans.size() && can_be < ans;\n        if (is_ans) ans = can_be;\n        one_count -= s[left++] == \'1\';\n    }\n    return ans;\n}\n\n/*\nResult\n\nRuntime: 5 ms, faster than 98.74% of C++ online submissions for Shortest \nand Lexicographically Smallest Beautiful String.\n\nMemory Usage: 6.7 MB, less than 100.00% of C++ online submissions for \nShortest and Lexicographically Smallest Beautiful String.\n\n*/\n```\n\n**Complexity**\n- Time: `O(n)`\n- Space: `O(1)`\n\n**For more solutions and concepts, check out this \uD83D\uDCD6 [BLOG](https://github.com/MuhtasimTanmoy/notebook)  and  \uD83C\uDFC6 [GITHUB REPOSITORY](https://github.com/MuhtasimTanmoy/playground) with over 2000+ solutions.**\n\n
['C', 'Sliding Window']