Sliding Window | C++ | Solved
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    string shortestBeautifulSubstring(string s, int k) {\n        int ones = 0;\n        int len = INT_MAX;\n\n        int j = 0;\n        int i = 0;\n        vector<string> substrings;\n\n        while (j < s.size()) {\n            if (s[j] == \'1\') {\n                ones++;\n            }\n\n            while (ones == k) {\n                if (j - i + 1 < len) {\n                    substrings.clear();  // Clear the previous substrings.\n                    len = j - i + 1;\n                }\n\n                if (j - i + 1 == len) {\n                    substrings.push_back(s.substr(i, len));\n                }\n\n                if (s[i] == \'1\') {\n                    ones--;\n                }\n                i++;\n            }\n            j++;\n        }\n\n        if (substrings.empty()) {\n            return "";\n        }\n\n        sort(substrings.begin(), substrings.end());\n\n        return substrings[0];\n    }\n};\n\n```
['C++']