Link: https://leetcode.com/problems/shortest-and-lexicographically-smallest-beautiful-string/
Solution:
Topics: subarray, sliding window
Intuition
Not much to this problem except being mindful of edge cases. Keep a sliding window, a count of ones, and the current length of the result. While one_count == k
, shrink the window. While shrinking the window, if the length r - l + 1
is smaller than length of the current result, update the result to the current slice s[l:r+1]
.
If the length of the window is the same as the length of the current result, do a lexicographical compare on both strings and update the result.
NOTE: a frequency map is not needed here because the only frequency we care about is that of the 1’s…so this can simply be stored in an integer.
Implementation
Visual