Skip to content

Commit b9dbaae

Browse files
committed
solved: 345. Reverse Vowels of a String
Signed-off-by: rajput-hemant <[email protected]>
1 parent 59fc3f9 commit b9dbaae

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
impl Solution {
2+
pub fn reverse_vowels(s: String) -> String {
3+
let mut s = s.into_bytes();
4+
let (mut i, mut j) = (0, s.len() - 1);
5+
6+
while i < j {
7+
if !Self::is_vowel(s[i] as char) {
8+
i += 1;
9+
continue;
10+
}
11+
if !Self::is_vowel(s[j] as char) {
12+
j -= 1;
13+
continue;
14+
}
15+
s.swap(i, j);
16+
i += 1;
17+
j -= 1;
18+
}
19+
String::from_utf8(s).unwrap()
20+
21+
// let mut chars: Vec<char> = s.chars().collect();
22+
// let (mut i, mut j) = (0, chars.len() - 1);
23+
24+
// while i < j {
25+
// if !Self::is_vowel(chars[i]) {
26+
// i += 1;
27+
// continue;
28+
// }
29+
// if !Self::is_vowel(chars[j]) {
30+
// j -= 1;
31+
// continue;
32+
// }
33+
// chars.swap(i, j);
34+
// i += 1;
35+
// j -= 1;
36+
// }
37+
38+
// chars.into_iter().collect()
39+
}
40+
41+
fn is_vowel(ch: char) -> bool {
42+
match ch {
43+
'a' | 'e' | 'i' | 'o' | 'u' | 'A' | 'E' | 'I' | 'O' | 'U' => true,
44+
_ => false,
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)