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 ba46ac8

Browse files
feat: add solutions to lc problem: No.3170 (doocs#4466)
No.3170.Lexicographically Minimum String After Removing Stars
1 parent a774cc7 commit ba46ac8

File tree

4 files changed

+215
-0
lines changed

4 files changed

+215
-0
lines changed

‎solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/README.md‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,81 @@ function clearStars(s: string): string {
235235
}
236236
```
237237

238+
#### Rust
239+
240+
```rust
241+
impl Solution {
242+
pub fn clear_stars(s: String) -> String {
243+
let n = s.len();
244+
let s_bytes = s.as_bytes();
245+
let mut g: Vec<Vec<usize>> = vec![vec![]; 26];
246+
let mut rem = vec![false; n];
247+
let chars: Vec<char> = s.chars().collect();
248+
249+
for (i, &ch) in chars.iter().enumerate() {
250+
if ch == '*' {
251+
rem[i] = true;
252+
for j in 0..26 {
253+
if let Some(idx) = g[j].pop() {
254+
rem[idx] = true;
255+
break;
256+
}
257+
}
258+
} else {
259+
g[(ch as u8 - b'a') as usize].push(i);
260+
}
261+
}
262+
263+
chars
264+
.into_iter()
265+
.enumerate()
266+
.filter_map(|(i, ch)| if !rem[i] { Some(ch) } else { None })
267+
.collect()
268+
}
269+
}
270+
```
271+
272+
#### C#
273+
274+
```cs
275+
public class Solution {
276+
public string ClearStars(string s) {
277+
int n = s.Length;
278+
List<int>[] g = new List<int>[26];
279+
for (int i = 0; i < 26; i++) {
280+
g[i] = new List<int>();
281+
}
282+
283+
bool[] rem = new bool[n];
284+
for (int i = 0; i < n; i++) {
285+
char ch = s[i];
286+
if (ch == '*') {
287+
rem[i] = true;
288+
for (int j = 0; j < 26; j++) {
289+
if (g[j].Count > 0) {
290+
int idx = g[j][g[j].Count - 1];
291+
g[j].RemoveAt(g[j].Count - 1);
292+
rem[idx] = true;
293+
break;
294+
}
295+
}
296+
} else {
297+
g[ch - 'a'].Add(i);
298+
}
299+
}
300+
301+
var ans = new System.Text.StringBuilder();
302+
for (int i = 0; i < n; i++) {
303+
if (!rem[i]) {
304+
ans.Append(s[i]);
305+
}
306+
}
307+
308+
return ans.ToString();
309+
}
310+
}
311+
```
312+
238313
<!-- tabs:end -->
239314

240315
<!-- solution:end -->

‎solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/README_EN.md‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,81 @@ function clearStars(s: string): string {
233233
}
234234
```
235235

236+
#### Rust
237+
238+
```rust
239+
impl Solution {
240+
pub fn clear_stars(s: String) -> String {
241+
let n = s.len();
242+
let s_bytes = s.as_bytes();
243+
let mut g: Vec<Vec<usize>> = vec![vec![]; 26];
244+
let mut rem = vec![false; n];
245+
let chars: Vec<char> = s.chars().collect();
246+
247+
for (i, &ch) in chars.iter().enumerate() {
248+
if ch == '*' {
249+
rem[i] = true;
250+
for j in 0..26 {
251+
if let Some(idx) = g[j].pop() {
252+
rem[idx] = true;
253+
break;
254+
}
255+
}
256+
} else {
257+
g[(ch as u8 - b'a') as usize].push(i);
258+
}
259+
}
260+
261+
chars
262+
.into_iter()
263+
.enumerate()
264+
.filter_map(|(i, ch)| if !rem[i] { Some(ch) } else { None })
265+
.collect()
266+
}
267+
}
268+
```
269+
270+
#### C#
271+
272+
```cs
273+
public class Solution {
274+
public string ClearStars(string s) {
275+
int n = s.Length;
276+
List<int>[] g = new List<int>[26];
277+
for (int i = 0; i < 26; i++) {
278+
g[i] = new List<int>();
279+
}
280+
281+
bool[] rem = new bool[n];
282+
for (int i = 0; i < n; i++) {
283+
char ch = s[i];
284+
if (ch == '*') {
285+
rem[i] = true;
286+
for (int j = 0; j < 26; j++) {
287+
if (g[j].Count > 0) {
288+
int idx = g[j][g[j].Count - 1];
289+
g[j].RemoveAt(g[j].Count - 1);
290+
rem[idx] = true;
291+
break;
292+
}
293+
}
294+
} else {
295+
g[ch - 'a'].Add(i);
296+
}
297+
}
298+
299+
var ans = new System.Text.StringBuilder();
300+
for (int i = 0; i < n; i++) {
301+
if (!rem[i]) {
302+
ans.Append(s[i]);
303+
}
304+
}
305+
306+
return ans.ToString();
307+
}
308+
}
309+
```
310+
236311
<!-- tabs:end -->
237312

238313
<!-- solution:end -->
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
public class Solution {
2+
public string ClearStars(string s) {
3+
int n = s.Length;
4+
List<int>[] g = new List<int>[26];
5+
for (int i = 0; i < 26; i++) {
6+
g[i] = new List<int>();
7+
}
8+
9+
bool[] rem = new bool[n];
10+
for (int i = 0; i < n; i++) {
11+
char ch = s[i];
12+
if (ch == '*') {
13+
rem[i] = true;
14+
for (int j = 0; j < 26; j++) {
15+
if (g[j].Count > 0) {
16+
int idx = g[j][g[j].Count - 1];
17+
g[j].RemoveAt(g[j].Count - 1);
18+
rem[idx] = true;
19+
break;
20+
}
21+
}
22+
} else {
23+
g[ch - 'a'].Add(i);
24+
}
25+
}
26+
27+
var ans = new System.Text.StringBuilder();
28+
for (int i = 0; i < n; i++) {
29+
if (!rem[i]) {
30+
ans.Append(s[i]);
31+
}
32+
}
33+
34+
return ans.ToString();
35+
}
36+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
impl Solution {
2+
pub fn clear_stars(s: String) -> String {
3+
let n = s.len();
4+
let s_bytes = s.as_bytes();
5+
let mut g: Vec<Vec<usize>> = vec![vec![]; 26];
6+
let mut rem = vec![false; n];
7+
let chars: Vec<char> = s.chars().collect();
8+
9+
for (i, &ch) in chars.iter().enumerate() {
10+
if ch == '*' {
11+
rem[i] = true;
12+
for j in 0..26 {
13+
if let Some(idx) = g[j].pop() {
14+
rem[idx] = true;
15+
break;
16+
}
17+
}
18+
} else {
19+
g[(ch as u8 - b'a') as usize].push(i);
20+
}
21+
}
22+
23+
chars
24+
.into_iter()
25+
.enumerate()
26+
.filter_map(|(i, ch)| if !rem[i] { Some(ch) } else { None })
27+
.collect()
28+
}
29+
}

0 commit comments

Comments
(0)

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