Python solution explained in detail
Intuition\n\nThe approach asks for a solution with a window of size K. This should help identify that Sliding Window could be a possible solution.\n\n# Approach
# Intuition\n\nThe approach asks for a solution with a window of size K. This should help identify that Sliding Window could be a possible solution.\n\n# Approach\nThe solution aims to find the shortest and lexicographically smallest "beautiful" substring within a binary string s, where a beautiful substring is defined by containing exactly k occurrences of \'1\'. The approach can be summarized as follows:\n\nTracking \'1\'s: Iterate through the string s, keeping track of the indices of \'1\'s encountered in a list (oneList). This list acts as a dynamic window of the most recent occurrences of \'1\'s, enabling efficient checks for the presence of beautiful substrings.\n\nMaintaining the Size of the Window: Ensure that oneList never holds more than k indices. If the list exceeds this size, remove the oldest entry. This step ensures that only substrings with exactly k \'1\'s are considered for evaluation.\n\nIdentifying Beautiful Substrings: Each time oneList contains exactly k indices, a beautiful substring is identified. Its start and end are determined by the first and last indices in oneList, respectively.\n\nSelecting the Optimal Substring: Upon finding a beautiful substring, compare it with the current best result (result) based on length and lexicographical order. Update result if the new substring is either shorter or equal in length but lexicographically smaller.\n\nReturn the final result.\n\n# Complexity\n- Time complexity:\n \n    O(n) time complexity, where n is the length of s.\n\n- Space complexity:\n\n    O(n) due to the storage of indices in oneList\n\n# Code\n```\nclass Solution:\n    def shortestBeautifulSubstring(self, s: str, k: int) -> str:\n        # Initialize a list to keep track of indices of \'1\'s in the string\n        oneList = []\n        # Initialize the result string to an empty string\n        result = \'\'\n        # Start iterating through the string\n        i = 0\n        while(i < len(s)):\n            # If the current character is \'1\', add its index to oneList\n            if s[i] == \'1\':\n                oneList.append(i)\n            # If oneList has more than k elements, remove the oldest one (maintain window of size k)\n            if len(oneList) > k:\n                oneList.pop(0)\n            # If oneList has exactly k elements, we\'ve found a beautiful substring\n            if len(oneList) == k:\n                # Extract the current beautiful substring\n                currStr = s[oneList[0]:oneList[-1] + 1]\n                # If result is empty or current substring is shorter, update the result\n                if result == \'\' or len(currStr) < len(result):\n                    result = currStr\n                # If the current substring is lexicographically smaller but of the same length, update result\n                elif len(result) == len(currStr) and currStr < result:\n                    result = currStr\n            i += 1\n        # Return the shortest and lexicographically smallest beautiful substring\n        return result\n\n```
['Sliding Window', 'Python3']