Python 3   ||   6 lines, with explanation  ||   T/S: 98% / 99%
Here\'s the plan:\n\nAn analysis of the problem leads to this condition: the first and last characters of the solution must each be one. So:\n\n1. We collect th
Here\'s the plan:\n\nAn analysis of the problem leads to this condition: the first and last characters of the solution must each be one. So:\n\n1. We collect the indices of ones in `ones`.\n2. We determine the delimiting indices `l` and `r` of those candidate strings in`cands` that (a) start and end with one and (b) contain`k`ones.\n2. We determine `minLen`, the minimum `r-l` of elements in `cands`.\n3. We filter`cands`to only include those candidates for which `l-r = minLen`\n4. We determine the lexicographically smallest substring with`min`.\n\n```\nclass Solution:\n    def shortestBeautifulSubstring(self, s: str, k: int) -> str:\n\n        ones = [i for i, digit in enumerate(s) if digit == \'1\']     # <-- 1.\n        \n        if len(ones) < k: return \'\'\n\n        cands = list(zip(ones,ones[k-1:]))                          \n        minLen = min(r-l for l, r in cands)                         # <-- 2.\n\n        cands = list(filter(lambda x: x[1]-x[0] == minLen, cands))  # <-- 3.\n\n        return min([s[l:r+1] for l,r in cands])                     # <-- 4.\n```\n[https://leetcode.com/problems/shortest-and-lexicographically-smallest-beautiful-string/submissions/1272097085/](https://leetcode.com/problems/shortest-and-lexicographically-smallest-beautiful-string/submissions/1272097085/)\n\nI could be wrong, but I think that time complexity is *O*(*N*) and space complexity is *O*(*N*), in which *N* ~ `len(s)`.\n\n\n
['Python3']