feat(SortVowels): add C# solution
This commit is contained in:
70
problems/2785-sort-vowels-in-a-string/README.md
Normal file
70
problems/2785-sort-vowels-in-a-string/README.md
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
# [2785] Sort Vowels In A String
|
||||||
|
|
||||||
|
## 題目資訊
|
||||||
|
- **難度**: Medium
|
||||||
|
- **標籤**: String, Sorting
|
||||||
|
- **題目連結**: [LeetCode](https://leetcode.com/problems/sort-vowels-in-a-string/)
|
||||||
|
- **練習日期**: 2025-09-11
|
||||||
|
|
||||||
|
## 題目描述
|
||||||
|
給定一個 0-indexed 的字串 s,重新排列 s 以獲得新字串 t,使得:
|
||||||
|
|
||||||
|
所有子音保持在原來的位置
|
||||||
|
母音必須按照 ASCII 值的非遞減順序排序
|
||||||
|
|
||||||
|
更正式地說:
|
||||||
|
|
||||||
|
如果存在索引 i,其中 0 <= i < s.length 且 s[i] 是子音,那麼 t[i] = s[i]
|
||||||
|
對於索引對 i, j,其中 0 <= i < j < s.length 且 s[i] 和 s[j] 都是母音,那麼 t[i] 的 ASCII 值不能高於 t[j]
|
||||||
|
|
||||||
|
母音定義: 'a', 'e', 'i', 'o', 'u' 以及它們的大寫形式
|
||||||
|
|
||||||
|
## 解題思路
|
||||||
|
|
||||||
|
### 初步分析
|
||||||
|
- 這題主要考察什麼概念?
|
||||||
|
這題主要考察字串處理和排序概念
|
||||||
|
- 有什麼關鍵限制條件?
|
||||||
|
- 預期時間/空間複雜度?
|
||||||
|
O(n log k) / O(k),其中 n 為字串長度,k 為母音數量
|
||||||
|
|
||||||
|
### 解法概述
|
||||||
|
1. **解法**:
|
||||||
|
- 思路:
|
||||||
|
1. 遍歷字串,收集所有母音及其位置
|
||||||
|
2. 對母音字符進行排序
|
||||||
|
3. 將排序後的母音按原位置重新放回字串
|
||||||
|
- 時間複雜度:O(n + k log k) 其中 n 是字串長度,k 是母音數量
|
||||||
|
- 空間複雜度:O(k) 存儲母音字符和位置
|
||||||
|
|
||||||
|
## 測試案例
|
||||||
|
|
||||||
|
### 範例輸入輸出
|
||||||
|
```
|
||||||
|
Input: s = "lEetcOde"
|
||||||
|
Output: "lEOtcede"
|
||||||
|
Explanation:
|
||||||
|
母音 'E', 'e', 'O', 'e' 排序後變成 'E', 'O', 'e', 'e'
|
||||||
|
```
|
||||||
|
|
||||||
|
### 邊界情況
|
||||||
|
- `1 <= s.length <= 10^5`
|
||||||
|
- `s` consists only of letters of the English alphabet in uppercase and lowercase.
|
||||||
|
|
||||||
|
## 學習筆記
|
||||||
|
|
||||||
|
### 今天學到什麼?
|
||||||
|
-
|
||||||
|
|
||||||
|
### 遇到的困難
|
||||||
|
-
|
||||||
|
|
||||||
|
### 改善方向
|
||||||
|
-
|
||||||
|
|
||||||
|
### 相關題目
|
||||||
|
- [題目編號] 題目名稱 - 相似概念
|
||||||
|
- [題目編號] 題目名稱 - 進階版本
|
||||||
|
|
||||||
|
---
|
||||||
|
**總結**: 這題的核心概念是...,適合練習...技巧。
|
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}");
|
||||||
|
}
|
||||||
|
}
|
10
problems/2785-sort-vowels-in-a-string/csharp/csharp.csproj
Normal file
10
problems/2785-sort-vowels-in-a-string/csharp/csharp.csproj
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
Reference in New Issue
Block a user