Skip to content

Commit 251474b

Browse files
authored
feat: add rust solution to lc problem: No.2785 (#4713)
1 parent dbf0f24 commit 251474b

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

solution/2700-2799/2785.Sort Vowels in a String/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,33 @@ function sortVowels(s: string): string {
182182
}
183183
```
184184

185+
#### Rust
186+
187+
```rust
188+
impl Solution {
189+
pub fn sort_vowels(s: String) -> String {
190+
fn is_vowel(c: char) -> bool {
191+
matches!(c.to_ascii_lowercase(), 'a' | 'e' | 'i' | 'o' | 'u')
192+
}
193+
194+
let mut vs: Vec<char> = s.chars().filter(|&c| is_vowel(c)).collect();
195+
vs.sort_unstable();
196+
197+
let mut cs: Vec<char> = s.chars().collect();
198+
let mut j = 0;
199+
200+
for (i, c) in cs.clone().into_iter().enumerate() {
201+
if is_vowel(c) {
202+
cs[i] = vs[j];
203+
j += 1;
204+
}
205+
}
206+
207+
cs.into_iter().collect()
208+
}
209+
}
210+
```
211+
185212
#### C#
186213

187214
```cs

solution/2700-2799/2785.Sort Vowels in a String/README_EN.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,33 @@ function sortVowels(s: string): string {
180180
}
181181
```
182182

183+
#### Rust
184+
185+
```rust
186+
impl Solution {
187+
pub fn sort_vowels(s: String) -> String {
188+
fn is_vowel(c: char) -> bool {
189+
matches!(c.to_ascii_lowercase(), 'a' | 'e' | 'i' | 'o' | 'u')
190+
}
191+
192+
let mut vs: Vec<char> = s.chars().filter(|&c| is_vowel(c)).collect();
193+
vs.sort_unstable();
194+
195+
let mut cs: Vec<char> = s.chars().collect();
196+
let mut j = 0;
197+
198+
for (i, c) in cs.clone().into_iter().enumerate() {
199+
if is_vowel(c) {
200+
cs[i] = vs[j];
201+
j += 1;
202+
}
203+
}
204+
205+
cs.into_iter().collect()
206+
}
207+
}
208+
```
209+
183210
#### C#
184211

185212
```cs
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn sort_vowels(s: String) -> String {
3+
fn is_vowel(c: char) -> bool {
4+
matches!(c.to_ascii_lowercase(), 'a' | 'e' | 'i' | 'o' | 'u')
5+
}
6+
7+
let mut vs: Vec<char> = s.chars().filter(|&c| is_vowel(c)).collect();
8+
vs.sort_unstable();
9+
10+
let mut cs: Vec<char> = s.chars().collect();
11+
let mut j = 0;
12+
13+
for (i, c) in cs.clone().into_iter().enumerate() {
14+
if is_vowel(c) {
15+
cs[i] = vs[j];
16+
j += 1;
17+
}
18+
}
19+
20+
cs.into_iter().collect()
21+
}
22+
}

0 commit comments

Comments
 (0)