diff --git a/solution/2700-2799/2785.Sort Vowels in a String/README.md b/solution/2700-2799/2785.Sort Vowels in a String/README.md index edb9599ed0f8b..9680ee488c319 100644 --- a/solution/2700-2799/2785.Sort Vowels in a String/README.md +++ b/solution/2700-2799/2785.Sort Vowels in a String/README.md @@ -163,6 +163,34 @@ function sortVowels(s: string): string { } ``` +### **C#** + +```cs +public class Solution { + public string SortVowels(string s) { + List vs = new List(); + char[] cs = s.ToCharArray(); + foreach (char c in cs) { + if (IsVowel(c)) { + vs.Add(c); + } + } + vs.Sort(); + for (int i = 0, j = 0; i < cs.Length; ++i) { + if (IsVowel(cs[i])) { + cs[i] = vs[j++]; + } + } + return new string(cs); + } + + public bool IsVowel(char c) { + c = char.ToLower(c); + return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; + } +} +``` + ### **...** ``` diff --git a/solution/2700-2799/2785.Sort Vowels in a String/README_EN.md b/solution/2700-2799/2785.Sort Vowels in a String/README_EN.md index f049e61593892..8355285940173 100644 --- a/solution/2700-2799/2785.Sort Vowels in a String/README_EN.md +++ b/solution/2700-2799/2785.Sort Vowels in a String/README_EN.md @@ -153,6 +153,34 @@ function sortVowels(s: string): string { } ``` +### **C#** + +```cs +public class Solution { + public string SortVowels(string s) { + List vs = new List(); + char[] cs = s.ToCharArray(); + foreach (char c in cs) { + if (IsVowel(c)) { + vs.Add(c); + } + } + vs.Sort(); + for (int i = 0, j = 0; i < cs.Length; ++i) { + if (IsVowel(cs[i])) { + cs[i] = vs[j++]; + } + } + return new string(cs); + } + + public bool IsVowel(char c) { + c = char.ToLower(c); + return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; + } +} +``` + ### **...** ``` diff --git a/solution/2700-2799/2785.Sort Vowels in a String/Solution.cs b/solution/2700-2799/2785.Sort Vowels in a String/Solution.cs new file mode 100644 index 0000000000000..a115baf150316 --- /dev/null +++ b/solution/2700-2799/2785.Sort Vowels in a String/Solution.cs @@ -0,0 +1,23 @@ +public class Solution { + public string SortVowels(string s) { + List vs = new List(); + char[] cs = s.ToCharArray(); + foreach (char c in cs) { + if (IsVowel(c)) { + vs.Add(c); + } + } + vs.Sort(); + for (int i = 0, j = 0; i < cs.Length; ++i) { + if (IsVowel(cs[i])) { + cs[i] = vs[j++]; + } + } + return new string(cs); + } + + public bool IsVowel(char c) { + c = char.ToLower(c); + return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; + } +}