Skip to content

Practice Shortest and Lexicographically Smallest Beautiful String

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2904
LeetCode Problem

Shortest and Lexicographically Smallest Beautiful String

Problem Statement

You are given a binary string s and a positive integer k. A substring of s is beautiful if the number of 1's in it is exactly k. Let len be the length of the shortest beautiful substring. Return the lexicographically smallest beautiful substring of string s with length equal to len. If s doesn't contain a beautiful substring, return an empty string. A string a is lexicographically larger than a string b (of the same length) if in the first position where a and b differ, a has a character strictly larger than the corresponding character in b.

Source: Shortest and Lexicographically Smallest Beautiful String on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "100011001", k = 3
Output
"11001"

# Example 2

Input
s = "1011", k = 2
Output
"11"

# Example 3

Input
s = "000", k = 1
Output
""

Constraints

1 <= s.length <= 100
1 <= k <= s.length

Related LeetCode Problems

Practice more problems similar to Shortest and Lexicographically Smallest Beautiful String.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.