Shortest and Lexicographically Smallest Beautiful String - Problem
Find the Shortest Beautiful Substring

You're given a binary string s containing only '0's and '1's, and a positive integer k. Your task is to find a special substring called a "beautiful substring".

A substring is beautiful if it contains exactly k ones ('1's). Among all beautiful substrings, you need to:
1. Find the minimum length of any beautiful substring
2. Among all beautiful substrings with this minimum length, return the lexicographically smallest one

If no beautiful substring exists, return an empty string.

Example: For s = "100110" and k = 2, the beautiful substrings are "10011", "00110", "0011", "011". The shortest length is 3, and "011" is lexicographically smallest among length-3 beautiful substrings.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "100110", k = 2
โ€บ Output: "11"
๐Ÿ’ก Note: Beautiful substrings with exactly 2 ones: "1001" (length 4), "0011" (length 4), "11" (length 2). The shortest length is 2, and "11" is the only substring of length 2.
example_2.py โ€” Multiple Same Length
$ Input: s = "1011", k = 2
โ€บ Output: "01"
๐Ÿ’ก Note: Beautiful substrings: "10" (length 2), "01" (length 2), "11" (length 2), "101" (length 3), "011" (length 3), "1011" (length 4). Among length-2 substrings, "01" is lexicographically smallest.
example_3.py โ€” No Solution
$ Input: s = "000", k = 1
โ€บ Output: ""
๐Ÿ’ก Note: The string contains no 1's, so no beautiful substring with exactly 1 one can be formed.

Constraints

  • 1 โ‰ค s.length โ‰ค 100
  • 1 โ‰ค k โ‰ค s.length
  • s consists only of '0' and '1'
  • The number of 1's in s is at least k for a solution to exist

Visualization

Tap to expand
Beautiful Substring Discovery Process100110String: "100110", k = 2Window 1: "1001" (length 4)2 ones found โœ“Window 2: "11" (length 2)2 ones found โœ“ - SHORTER!Algorithm Steps:1. Start from each '1' position (lexicographic optimization)2. Expand window until exactly k ones are collected3. Compare length with current best (prefer shorter)4. If same length, prefer lexicographically smaller substringResult: "11" - Shortest and Lexicographically Smallest!Time Complexity: O(nยฒ) | Space Complexity: O(n)
Understanding the Visualization
1
Identify Pearl Positions
Mark all positions containing pearls (1's) in the binary string
2
Start Collection
Begin collecting from each pearl position, extending the net until you have k pearls
3
Measure and Compare
For each valid collection, measure its length and lexicographic value
4
Select the Best
Choose the shortest collection, and if tied, the lexicographically smallest
Key Takeaway
๐ŸŽฏ Key Insight: By starting our search only from positions containing '1' and using a sliding window approach, we efficiently find the shortest beautiful substring while ensuring lexicographic minimality.
Asked in
Google 15 Meta 12 Amazon 10 Microsoft 8
28.4K Views
Medium Frequency
~18 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen