C++ || Sliding Window
Intuition\n Describe your first thoughts on how to solve this problem. \nQuestion is asking to find the minimum length substring which is lexicographically smal
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nQuestion is asking to find the minimum length substring which is lexicographically smallest as well.\nSince substring is a contiguous set of characters, we can apply sliding window to get the answer\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Take two pointers i and j, and a counter variable to track number of 1\'s\n2. Increment counter if s[j]==\'1\'\n3. if cnt>k or s[i]==\'0\' increase the i pointer and decrease cnt if s[i]==\'1\'\n4. if cnt==k, check if current length < minLength, if yes, store that substring in ans\n5. if current length==minLength, compare if current substring is lexicographically smaller than the answer string, if yes, store current in answer, otherwise move on to next iteration.\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\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 n = s.size();\n        int i = 0, j = 0;\n        int cnt = 0, minL = 1e9;\n        string ans = "";\n        \n        while (j < n) {\n            if (s[j] == \'1\') cnt++;\n            \n            while (cnt > k || s[i]==\'0\') {\n                if (s[i] == \'1\') cnt--;\n                i++;\n            }\n            \n            if (cnt == k) {\n                int currLen = j - i + 1;\n                string temp=s.substr(i,currLen);\n                if (currLen < minL) {\n                    minL = currLen;\n                    ans = temp;\n                }\n                else if(currLen==minL && temp<ans){\n                    ans=temp;\n                }\n            }\n            \n            j++;\n        }\n        \n        return ans;\n    }\n};\n\n```
['Sliding Window', 'C++']