Class Solution
-
- All Implemented Interfaces:
public final class Solution2904 - Shortest and Lexicographically Smallest Beautiful String.
Medium
You are given a binary string
sand a positive integerk.A substring of
sis beautiful if the number of1's in it is exactlyk.Let
lenbe the length of the shortest beautiful substring.Return the lexicographically smallest beautiful substring of string
swith length equal tolen. Ifsdoesn't contain a beautiful substring, return an empty string.A string
ais lexicographically larger than a stringb(of the same length) if in the first position whereaandbdiffer,ahas a character strictly larger than the corresponding character inb.For example,
"abcd"is lexicographically larger than"abcc"because the first position they differ is at the fourth character, anddis greater thanc.
Example 1:
Input: s = "100011001", k = 3
Output: "11001"
Explanation: There are 7 beautiful substrings in this example:
The substring "<ins>100011</ins>001".
The substring "<ins>1000110</ins>01".
The substring "<ins>10001100</ins>1".
The substring "1<ins>00011001</ins>".
The substring "10<ins>0011001</ins>".
The substring "100<ins>011001</ins>".
The substring "1000<ins>11001</ins>".
The length of the shortest beautiful substring is 5.
The lexicographically smallest beautiful substring with length 5 is the substring "11001".
Example 2:
Input: s = "1011", k = 2
Output: "11"
Explanation: There are 3 beautiful substrings in this example:
The substring "<ins>101</ins>1".
The substring "1<ins>011</ins>".
The substring "10<ins>11</ins>".
The length of the shortest beautiful substring is 2.
The lexicographically smallest beautiful substring with length 2 is the substring "11".
Example 3:
Input: s = "000", k = 1
Output: ""
Explanation: There are no beautiful substrings in this example.
Constraints:
1 <= s.length <= 1001 <= k <= s.length
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final StringshortestBeautifulSubstring(String s, Integer k)-
-
Method Detail
-
shortestBeautifulSubstring
final String shortestBeautifulSubstring(String s, Integer k)
-
-
-
-