From 549270f4f1b028168b1e239a5dfee303c5e8b073 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 7 Jun 2025 09:44:32 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3170 No.3170.Lexicographically Minimum String After Removing Stars --- .../README.md | 75 +++++++++++++++++++ .../README_EN.md | 75 +++++++++++++++++++ .../Solution.cs | 36 +++++++++ .../Solution.rs | 29 +++++++ 4 files changed, 215 insertions(+) create mode 100644 solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/Solution.cs create mode 100644 solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/Solution.rs diff --git a/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/README.md b/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/README.md index 8d1aa6931366a..93a97c1f879da 100644 --- a/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/README.md +++ b/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/README.md @@ -235,6 +235,81 @@ function clearStars(s: string): string { } ``` +#### Rust + +```rust +impl Solution { + pub fn clear_stars(s: String) -> String { + let n = s.len(); + let s_bytes = s.as_bytes(); + let mut g: Vec> = vec![vec![]; 26]; + let mut rem = vec![false; n]; + let chars: Vec = s.chars().collect(); + + for (i, &ch) in chars.iter().enumerate() { + if ch == '*' { + rem[i] = true; + for j in 0..26 { + if let Some(idx) = g[j].pop() { + rem[idx] = true; + break; + } + } + } else { + g[(ch as u8 - b'a') as usize].push(i); + } + } + + chars + .into_iter() + .enumerate() + .filter_map(|(i, ch)| if !rem[i] { Some(ch) } else { None }) + .collect() + } +} +``` + +#### C# + +```cs +public class Solution { + public string ClearStars(string s) { + int n = s.Length; + List[] g = new List[26]; + for (int i = 0; i < 26; i++) { + g[i] = new List(); + } + + bool[] rem = new bool[n]; + for (int i = 0; i < n; i++) { + char ch = s[i]; + if (ch == '*') { + rem[i] = true; + for (int j = 0; j < 26; j++) { + if (g[j].Count > 0) { + int idx = g[j][g[j].Count - 1]; + g[j].RemoveAt(g[j].Count - 1); + rem[idx] = true; + break; + } + } + } else { + g[ch - 'a'].Add(i); + } + } + + var ans = new System.Text.StringBuilder(); + for (int i = 0; i < n; i++) { + if (!rem[i]) { + ans.Append(s[i]); + } + } + + return ans.ToString(); + } +} +``` + diff --git a/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/README_EN.md b/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/README_EN.md index 1703505a8e3d5..1e3a403d76cb1 100644 --- a/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/README_EN.md +++ b/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/README_EN.md @@ -233,6 +233,81 @@ function clearStars(s: string): string { } ``` +#### Rust + +```rust +impl Solution { + pub fn clear_stars(s: String) -> String { + let n = s.len(); + let s_bytes = s.as_bytes(); + let mut g: Vec> = vec![vec![]; 26]; + let mut rem = vec![false; n]; + let chars: Vec = s.chars().collect(); + + for (i, &ch) in chars.iter().enumerate() { + if ch == '*' { + rem[i] = true; + for j in 0..26 { + if let Some(idx) = g[j].pop() { + rem[idx] = true; + break; + } + } + } else { + g[(ch as u8 - b'a') as usize].push(i); + } + } + + chars + .into_iter() + .enumerate() + .filter_map(|(i, ch)| if !rem[i] { Some(ch) } else { None }) + .collect() + } +} +``` + +#### C# + +```cs +public class Solution { + public string ClearStars(string s) { + int n = s.Length; + List[] g = new List[26]; + for (int i = 0; i < 26; i++) { + g[i] = new List(); + } + + bool[] rem = new bool[n]; + for (int i = 0; i < n; i++) { + char ch = s[i]; + if (ch == '*') { + rem[i] = true; + for (int j = 0; j < 26; j++) { + if (g[j].Count > 0) { + int idx = g[j][g[j].Count - 1]; + g[j].RemoveAt(g[j].Count - 1); + rem[idx] = true; + break; + } + } + } else { + g[ch - 'a'].Add(i); + } + } + + var ans = new System.Text.StringBuilder(); + for (int i = 0; i < n; i++) { + if (!rem[i]) { + ans.Append(s[i]); + } + } + + return ans.ToString(); + } +} +``` + diff --git a/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/Solution.cs b/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/Solution.cs new file mode 100644 index 0000000000000..2d0a9917622ed --- /dev/null +++ b/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/Solution.cs @@ -0,0 +1,36 @@ +public class Solution { + public string ClearStars(string s) { + int n = s.Length; + List[] g = new List[26]; + for (int i = 0; i < 26; i++) { + g[i] = new List(); + } + + bool[] rem = new bool[n]; + for (int i = 0; i < n; i++) { + char ch = s[i]; + if (ch == '*') { + rem[i] = true; + for (int j = 0; j < 26; j++) { + if (g[j].Count > 0) { + int idx = g[j][g[j].Count - 1]; + g[j].RemoveAt(g[j].Count - 1); + rem[idx] = true; + break; + } + } + } else { + g[ch - 'a'].Add(i); + } + } + + var ans = new System.Text.StringBuilder(); + for (int i = 0; i < n; i++) { + if (!rem[i]) { + ans.Append(s[i]); + } + } + + return ans.ToString(); + } +} diff --git a/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/Solution.rs b/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/Solution.rs new file mode 100644 index 0000000000000..26cb736f498a8 --- /dev/null +++ b/solution/3100-3199/3170.Lexicographically Minimum String After Removing Stars/Solution.rs @@ -0,0 +1,29 @@ +impl Solution { + pub fn clear_stars(s: String) -> String { + let n = s.len(); + let s_bytes = s.as_bytes(); + let mut g: Vec> = vec![vec![]; 26]; + let mut rem = vec![false; n]; + let chars: Vec = s.chars().collect(); + + for (i, &ch) in chars.iter().enumerate() { + if ch == '*' { + rem[i] = true; + for j in 0..26 { + if let Some(idx) = g[j].pop() { + rem[idx] = true; + break; + } + } + } else { + g[(ch as u8 - b'a') as usize].push(i); + } + } + + chars + .into_iter() + .enumerate() + .filter_map(|(i, ch)| if !rem[i] { Some(ch) } else { None }) + .collect() + } +}