cs: add «345. Reverse Vowels of a String»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
ef40cbb1fd
commit
97bc690403
1 changed files with 26 additions and 0 deletions
26
cs/reverse-vowels-of-a-string.cs
Normal file
26
cs/reverse-vowels-of-a-string.cs
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
public class Solution {
|
||||||
|
private bool IsVowel(char c) {
|
||||||
|
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
|
||||||
|
}
|
||||||
|
|
||||||
|
public string ReverseVowels(string s) {
|
||||||
|
var reversed = new StringBuilder(s);
|
||||||
|
|
||||||
|
s = s.ToLower();
|
||||||
|
for (int l = 0, r = reversed.Length - 1; l < r; ++l, --r) {
|
||||||
|
while (l < reversed.Length && !IsVowel(s[l])) {
|
||||||
|
++l;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (r > 0 && !IsVowel(s[r])) {
|
||||||
|
--r;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (l < r) {
|
||||||
|
(reversed[l], reversed[r]) = (reversed[r], reversed[l]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return reversed.ToString();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue