Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7553442

Browse files
feat: add solutions to lc problem: No.2904 (#1820)
No.2904.Shortest and Lexicographically Smallest Beautiful String
1 parent 7320586 commit 7553442

File tree

8 files changed

+649
-0
lines changed

8 files changed

+649
-0
lines changed

‎solution/2900-2999/2904.Shortest and Lexicographically Smallest Beautiful String/README.md‎

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,34 +73,297 @@
7373

7474
<!-- 这里可写通用的实现逻辑 -->
7575

76+
**方法一:枚举**
77+
78+
我们可以枚举所有子字符串 $s[i: j],ドル其中 $i \lt j,ドル并检查它们是否是美丽子字符串。如果是,我们就更新答案。
79+
80+
时间复杂度 $O(n^3),ドル空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。
81+
82+
**方法二:双指针**
83+
84+
我们也可以用两个指针维护一个滑动窗口,其中指针 $i$ 指向滑动窗口的左端点,指针 $j$ 指向滑动窗口的右端点。初始时 $i = j = 0$。另外,我们用变量 $cnt$ 记录滑动窗口中的 1ドル$ 的个数。
85+
86+
我们首先将指针 $j$ 向右移动,将 $s[j]$ 加入到滑动窗口中,并更新 $cnt$。如果 $cnt \gt k,ドル或者 $i \lt j$ 并且 $s[i]=0,ドル我们就循环将指针 $i$ 往右移动,并且更新 $cnt$。
87+
88+
当 $cnt = k$ 时,我们就找到了一个美丽子字符串。我们将它与当前的答案进行比较,并更新答案。
89+
90+
时间复杂度 $O(n^2),ドル空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。
91+
7692
<!-- tabs:start -->
7793

7894
### **Python3**
7995

8096
<!-- 这里可写当前语言的特殊实现逻辑 -->
8197

8298
```python
99+
class Solution:
100+
def shortestBeautifulSubstring(self, s: str, k: int) -> str:
101+
n = len(s)
102+
ans = ""
103+
for i in range(n):
104+
for j in range(i + k, n + 1):
105+
t = s[i:j]
106+
if t.count("1") == k and (
107+
not ans or j - i < len(ans) or (j - i == len(ans) and t < ans)
108+
):
109+
ans = t
110+
return ans
111+
```
83112

113+
```python
114+
class Solution:
115+
def shortestBeautifulSubstring(self, s: str, k: int) -> str:
116+
i = j = cnt = 0
117+
n = len(s)
118+
ans = ""
119+
while j < n:
120+
cnt += s[j] == "1"
121+
while cnt > k or (i < j and s[i] == "0"):
122+
cnt -= s[i] == "1"
123+
i += 1
124+
j += 1
125+
if cnt == k and (
126+
not ans or j - i < len(ans) or (j - i == len(ans) and s[i:j] < ans)
127+
):
128+
ans = s[i:j]
129+
return ans
84130
```
85131

86132
### **Java**
87133

88134
<!-- 这里可写当前语言的特殊实现逻辑 -->
89135

90136
```java
137+
class Solution {
138+
public String shortestBeautifulSubstring(String s, int k) {
139+
int n = s.length();
140+
String ans = "";
141+
for (int i = 0; i < n; ++i) {
142+
for (int j = i + k; j <= n; ++j) {
143+
String t = s.substring(i, j);
144+
int cnt = 0;
145+
for (char c : t.toCharArray()) {
146+
cnt += c - '0';
147+
}
148+
if (cnt == k && ("".equals(ans) || j - i < ans.length() || (j - i == ans.length() && t.compareTo(ans) < 0))) {
149+
ans = t;
150+
}
151+
}
152+
}
153+
return ans;
154+
}
155+
}
156+
```
91157

158+
```java
159+
class Solution {
160+
public String shortestBeautifulSubstring(String s, int k) {
161+
int i = 0, j = 0, cnt = 0;
162+
int n = s.length();
163+
String ans = "";
164+
while (j < n) {
165+
cnt += s.charAt(j) - '0';
166+
while (cnt > k || (i < j && s.charAt(i) == '0')) {
167+
cnt -= s.charAt(i) - '0';
168+
++i;
169+
}
170+
++j;
171+
String t = s.substring(i, j);
172+
if (cnt == k
173+
&& ("".equals(ans) || j - i < ans.length()
174+
|| (j - i == ans.length() && t.compareTo(ans) < 0))) {
175+
ans = t;
176+
}
177+
}
178+
return ans;
179+
}
180+
}
92181
```
93182

94183
### **C++**
95184

96185
```cpp
186+
class Solution {
187+
public:
188+
string shortestBeautifulSubstring(string s, int k) {
189+
int n = s.size();
190+
string ans = "";
191+
for (int i = 0; i < n; ++i) {
192+
for (int j = i + k; j <= n; ++j) {
193+
string t = s.substr(i, j - i);
194+
int cnt = count(t.begin(), t.end(), '1');
195+
if (cnt == k && (ans == "" || j - i < ans.size() || (j - i == ans.size() && t < ans))) {
196+
ans = t;
197+
}
198+
}
199+
}
200+
return ans;
201+
}
202+
};
203+
```
97204
205+
```cpp
206+
class Solution {
207+
public:
208+
string shortestBeautifulSubstring(string s, int k) {
209+
int i = 0, j = 0, cnt = 0;
210+
int n = s.size();
211+
string ans = "";
212+
while (j < n) {
213+
cnt += s[j] == '1';
214+
while (cnt > k || (i < j && s[i] == '0')) {
215+
cnt -= s[i++] == '1';
216+
}
217+
++j;
218+
string t = s.substr(i, j - i);
219+
if (cnt == k && (ans == "" || j - i < ans.size() || (j - i == ans.size() && t < ans))) {
220+
ans = t;
221+
}
222+
}
223+
return ans;
224+
}
225+
};
98226
```
99227

100228
### **Go**
101229

102230
```go
231+
func shortestBeautifulSubstring(s string, k int) (ans string) {
232+
n := len(s)
233+
for i := 0; i < n; i++ {
234+
for j := i + k; j <= n; j++ {
235+
t := s[i:j]
236+
cnt := 0
237+
for _, c := range t {
238+
if c == '1' {
239+
cnt++
240+
}
241+
}
242+
if cnt == k && (ans == "" || j-i < len(ans) || (j-i == len(ans) && t < ans)) {
243+
ans = t
244+
}
245+
}
246+
}
247+
return
248+
}
249+
```
250+
251+
```go
252+
func shortestBeautifulSubstring(s string, k int) (ans string) {
253+
i, j, cnt := 0, 0, 0
254+
n := len(s)
255+
for j < n {
256+
cnt += int(s[j] - '0')
257+
for cnt > k || (i < j && s[i] == '0') {
258+
cnt -= int(s[i] - '0')
259+
i++
260+
}
261+
j++
262+
t := s[i:j]
263+
if cnt == k && (ans == "" || j-i < len(ans) || (j-i == len(ans) && t < ans)) {
264+
ans = t
265+
}
266+
}
267+
return
268+
}
269+
```
270+
271+
### **TypeScript**
272+
273+
```ts
274+
function shortestBeautifulSubstring(s: string, k: number): string {
275+
const n = s.length;
276+
let ans: string = '';
277+
for (let i = 0; i < n; ++i) {
278+
for (let j = i + k; j <= n; ++j) {
279+
const t = s.slice(i, j);
280+
const cnt = t.split('').filter(c => c === '1').length;
281+
if (
282+
cnt === k &&
283+
(ans === '' || j - i < ans.length || (j - i === ans.length && t < ans))
284+
) {
285+
ans = t;
286+
}
287+
}
288+
}
289+
return ans;
290+
}
291+
```
292+
293+
```ts
294+
function shortestBeautifulSubstring(s: string, k: number): string {
295+
let [i, j, cnt] = [0, 0, 0];
296+
const n = s.length;
297+
let ans: string = '';
298+
while (j < n) {
299+
cnt += s[j] === '1' ? 1 : 0;
300+
while (cnt > k || (i < j && s[i] === '0')) {
301+
cnt -= s[i++] === '1' ? 1 : 0;
302+
}
303+
++j;
304+
const t = s.slice(i, j);
305+
if (cnt === k && (ans === '' || j - i < ans.length || (j - i === ans.length && t < ans))) {
306+
ans = t;
307+
}
308+
}
309+
return ans;
310+
}
311+
```
312+
313+
### **Rust**
314+
315+
```rust
316+
impl Solution {
317+
pub fn shortest_beautiful_substring(s: String, k: i32) -> String {
318+
let n = s.len();
319+
let mut ans = String::new();
320+
321+
for i in 0..n {
322+
for j in (i + k as usize)..=n {
323+
let t = &s[i..j];
324+
if t.matches('1').count() as i32 == k &&
325+
(ans.is_empty() || j - i < ans.len() || (j - i == ans.len() && t < &ans)) {
326+
ans = t.to_string();
327+
}
328+
}
329+
}
330+
ans
331+
}
332+
}
333+
```
103334

335+
```rust
336+
impl Solution {
337+
pub fn shortest_beautiful_substring(s: String, k: i32) -> String {
338+
let s_chars: Vec<char> = s.chars().collect();
339+
let mut i = 0;
340+
let mut j = 0;
341+
let mut cnt = 0;
342+
let mut ans = String::new();
343+
let n = s.len();
344+
345+
while j < n {
346+
if s_chars[j] == '1' {
347+
cnt += 1;
348+
}
349+
350+
while cnt > k || (i < j && s_chars[i] == '0') {
351+
if s_chars[i] == '1' {
352+
cnt -= 1;
353+
}
354+
i += 1;
355+
}
356+
357+
j += 1;
358+
359+
if cnt == k && (ans.is_empty() || j - i < ans.len() || (j - i == ans.len() && &s[i..j] < &ans)) {
360+
ans = s_chars[i..j].iter().collect();
361+
}
362+
}
363+
364+
ans
365+
}
366+
}
104367
```
105368

106369
### **...**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /