2904. Shortest and Lexicographically Smallest Beautiful String
</> Solution
/**
* Problem: LeetCode 2904 - Shortest and Lexicographically Smallest Beautiful String
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public String shortestBeautifulSubstring(String s, int k) {
int n = s.length();
String result = "";
int minLen = Integer.MAX_VALUE;
int left = 0, ones = 0;
for (int right = 0; right < n; right++) {
if (s.charAt(right) == '1') {
ones++;
}
// shrink from left while window has more than k ones,
// or while shrinking still keeps exactly k ones
while (ones > k || (left <= right && s.charAt(left) == '0' && ones == k)) {
if (s.charAt(left) == '1') {
ones--;
}
left++;
}
if (ones == k) {
int len = right - left + 1;
String candidate = s.substring(left, right + 1);
if (len < minLen || (len == minLen && candidate.compareTo(result) < 0)) {
minLen = len;
result = candidate;
}
}
}
return result;
}
}
"""
Problem: LeetCode 2904 - Shortest and Lexicographically Smallest Beautiful String
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
class Solution:
def shortestBeautifulSubstring(self, s: str, k: int) -> str:
n = len(s)
result = ""
min_len = float("inf")
left = 0
ones = 0
for right in range(n):
if s[right] == "1":
ones += 1
# shrink while window has more than k ones or has leading zeros with exactly k ones
while ones > k or (left <= right and s[left] == "0" and ones == k):
if s[left] == "1":
ones -= 1
left += 1
if ones == k:
length = right - left + 1
candidate = s[left : right + 1]
if length < min_len or (length == min_len and candidate < result):
min_len = length
result = candidate
return result
/**
* Problem: LeetCode 2904 - Shortest and Lexicographically Smallest Beautiful String
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <string>
#include <climits>
using namespace std;
/**
* Problem: LeetCode 2904 - Shortest and Lexicographically Smallest Beautiful String
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
string shortestBeautifulSubstring(string s, int k) {
int n = s.length();
string result = "";
int minLen = INT_MAX;
int left = 0, ones = 0;
for (int right = 0; right < n; ++right) {
if (s[right] == '1') {
ones++;
}
// shrink from left while window has more than k ones,
// or while shrinking still keeps exactly k ones
while (ones > k || (left <= right && s[left] == '0' && ones == k)) {
if (s[left] == '1') {
ones--;
}
left++;
}
if (ones == k) {
int len = right - left + 1;
string candidate = s.substr(left, len);
if (len < minLen || (len == minLen && candidate < result)) {
minLen = len;
result = candidate;
}
}
}
return result;
}
};
/**
* Problem: LeetCode 2904 - Shortest and Lexicographically Smallest Beautiful String
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var shortestBeautifulSubstring = function(s, k) {
const n = s.length;
let result = "";
let minLen = Infinity;
let left = 0, ones = 0;
for (let right = 0; right < n; right++) {
if (s[right] === '1') {
ones++;
}
// shrink from left while window has more than k ones,
// or while shrinking still keeps exactly k ones
while (ones > k || (left <= right && s[left] === '0' && ones === k)) {
if (s[left] === '1') {
ones--;
}
left++;
}
if (ones === k) {
const len = right - left + 1;
const candidate = s.substring(left, right + 1);
if (len < minLen || (len === minLen && candidate < result)) {
minLen = len;
result = candidate;
}
}
}
return result;
};