feat(SortVowels): add C# solution
This commit is contained in:
108
problems/2785-sort-vowels-in-a-string/csharp/Program.cs
Normal file
108
problems/2785-sort-vowels-in-a-string/csharp/Program.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
// LeetCode 2785: Sort Vowels In A String
|
||||
// 難度: Medium
|
||||
// 日期: 2025-09-11
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public class Solution {
|
||||
public string SortVowels(string s) {
|
||||
// to char[]
|
||||
char[] sArray = s.ToCharArray();
|
||||
|
||||
// get all vowels
|
||||
string vowels = "aeiouAEIOU";
|
||||
List<int> vowelPositions = new List<int>();
|
||||
List<char> vowelChars = new List<char>();
|
||||
|
||||
for(int i = 0; i < sArray.Length; i++){
|
||||
if(vowels.Contains(sArray[i])){
|
||||
vowelPositions.Add(i);
|
||||
vowelChars.Add(sArray[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// sort all vowels
|
||||
vowelChars.Sort();
|
||||
|
||||
// insert vowels
|
||||
for(var i = 0; i < vowelPositions.Count; i++){
|
||||
sArray[vowelPositions[i]] = vowelChars[i];
|
||||
}
|
||||
|
||||
return new string(sArray);
|
||||
}
|
||||
}
|
||||
|
||||
// 測試程式
|
||||
public class Program {
|
||||
public static void Main() {
|
||||
Solution sol = new Solution();
|
||||
|
||||
// TODO: 加入測試案例
|
||||
TestCase1(sol);
|
||||
TestCase2(sol);
|
||||
TestCase3(sol);
|
||||
TestCase4(sol);
|
||||
TestCase5(sol);
|
||||
TestCase6(sol);
|
||||
TestCase7(sol);
|
||||
}
|
||||
|
||||
// 基本範例 - LeetCode官方例子
|
||||
static void TestCase1(Solution sol) {
|
||||
var s = "lEetcOde";
|
||||
var result = sol.SortVowels(s);
|
||||
var expected = "lEOtcede";
|
||||
Console.WriteLine($"Test 1: Input: \"{s}\", Result: \"{result}\", Expected: \"{expected}\", Pass: {result == expected}");
|
||||
}
|
||||
|
||||
// 只有子音 - 沒有母音的情況
|
||||
static void TestCase2(Solution sol) {
|
||||
var s = "lYmpH";
|
||||
var result = sol.SortVowels(s);
|
||||
var expected = "lYmpH";
|
||||
Console.WriteLine($"Test 2: Input: \"{s}\", Result: \"{result}\", Expected: \"{expected}\", Pass: {result == expected}");
|
||||
}
|
||||
|
||||
// 只有母音 - 測試排序功能
|
||||
static void TestCase3(Solution sol) {
|
||||
var s = "aEiOu";
|
||||
var result = sol.SortVowels(s);
|
||||
var expected = "EOaiu";
|
||||
Console.WriteLine($"Test 3: Input: \"{s}\", Result: \"{result}\", Expected: \"{expected}\", Pass: {result == expected}");
|
||||
}
|
||||
|
||||
// 混合大小寫母音
|
||||
static void TestCase4(Solution sol) {
|
||||
var s = "TestcasE";
|
||||
var result = sol.SortVowels(s);
|
||||
var expected = "TEstcase";
|
||||
Console.WriteLine($"Test 4: Input: \"{s}\", Result: \"{result}\", Expected: \"{expected}\", Pass: {result == expected}");
|
||||
}
|
||||
|
||||
// 單一字元 - 邊界情況
|
||||
static void TestCase5(Solution sol) {
|
||||
var s = "a";
|
||||
var result = sol.SortVowels(s);
|
||||
var expected = "a";
|
||||
Console.WriteLine($"Test 5: Input: \"{s}\", Result: \"{result}\", Expected: \"{expected}\", Pass: {result == expected}");
|
||||
}
|
||||
|
||||
// 重複母音
|
||||
static void TestCase6(Solution sol) {
|
||||
var s = "Aa";
|
||||
var result = sol.SortVowels(s);
|
||||
var expected = "Aa";
|
||||
Console.WriteLine($"Test 6: Input: \"{s}\", Result: \"{result}\", Expected: \"{expected}\", Pass: {result == expected}");
|
||||
}
|
||||
|
||||
// 較長的字串測試
|
||||
static void TestCase7(Solution sol) {
|
||||
var s = "RaInBoW";
|
||||
var result = sol.SortVowels(s);
|
||||
var expected = "RIanBoW";
|
||||
Console.WriteLine($"Test 7: Input: \"{s}\", Result: \"{result}\", Expected: \"{expected}\", Pass: {result == expected}");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user