[#3227] feat(leetcode): add c# and golang solution

This commit is contained in:
2025-09-12 13:13:22 +08:00
parent 8dfafaaf43
commit 1e562065e8
10 changed files with 561 additions and 0 deletions

View File

@@ -0,0 +1,168 @@
// LeetCode 3227: Vowels Game in a String 單元測試xUnit
// Problem: Alice and Bob play a game. Alice removes substrings with odd vowels,
// Bob removes substrings with even vowels. Alice wins if string has any vowels.
using Xunit;
public class SolutionTests
{
private readonly Solution _s = new Solution();
[Fact]
public void Example1_StringWithVowels_AliceWins()
{
// Arrange
var input = "leetcode"; // contains vowels: e,e,o,e
var expected = true; // Alice wins
// Act
var got = _s.DoesAliceWin(input);
// Assert
Assert.Equal(expected, got);
}
[Fact]
public void Example2_StringWithoutVowels_BobWins()
{
// Arrange
var input = "bcdf"; // no vowels
var expected = false; // Bob wins (Alice can't make first move)
// Act
var got = _s.DoesAliceWin(input);
// Assert
Assert.Equal(expected, got);
}
[Fact]
public void AllVowels_AliceWins()
{
// Arrange
var input = "aeiou"; // all vowels (odd count = 5)
var expected = true; // Alice removes entire string
// Act
var got = _s.DoesAliceWin(input);
// Assert
Assert.Equal(expected, got);
}
[Fact]
public void SingleVowel_AliceWins()
{
// Arrange
var input = "a"; // single vowel
var expected = true; // Alice removes it immediately
// Act
var got = _s.DoesAliceWin(input);
// Assert
Assert.Equal(expected, got);
}
[Fact]
public void SingleConsonant_BobWins()
{
// Arrange
var input = "b"; // single consonant, no vowels
var expected = false; // Alice can't move
// Act
var got = _s.DoesAliceWin(input);
// Assert
Assert.Equal(expected, got);
}
[Fact]
public void MixedString_EvenVowelCount_AliceWins()
{
// Arrange
var input = "programming"; // vowels: o,a,i (3 vowels, odd)
var expected = true; // Alice can remove all vowels at once
// Act
var got = _s.DoesAliceWin(input);
// Assert
Assert.Equal(expected, got);
}
[Fact]
public void LongStringNoVowels_BobWins()
{
// Arrange
var input = "bcdfghjklmnpqrstvwxyz"; // all consonants
var expected = false; // No vowels = Bob wins
// Act
var got = _s.DoesAliceWin(input);
// Assert
Assert.Equal(expected, got);
}
[Fact]
public void RepeatedVowels_AliceWins()
{
// Arrange
var input = "aaaa"; // 4 vowels (even count)
var expected = true; // Alice can remove 3, leaving 1 (Bob can't move)
// Act
var got = _s.DoesAliceWin(input);
// Assert
Assert.Equal(expected, got);
}
[Fact]
public void EdgeCases()
{
// Single character tests
Assert.True(_s.DoesAliceWin("e")); // vowel
Assert.False(_s.DoesAliceWin("x")); // consonant
// Two character tests
Assert.True(_s.DoesAliceWin("ab")); // has vowel 'a'
Assert.False(_s.DoesAliceWin("xy")); // no vowels
// Vowel at different positions
Assert.True(_s.DoesAliceWin("xabc")); // vowel at start-middle
Assert.True(_s.DoesAliceWin("abcx")); // vowel in middle
Assert.True(_s.DoesAliceWin("xbca")); // vowel at end
}
[Fact]
public void GameTheoryValidation()
{
// Test cases that validate the game theory logic:
// If total vowels = 0: Alice loses (can't make first move)
Assert.False(_s.DoesAliceWin("bcdfg"));
// If total vowels = odd: Alice wins (removes all vowels in one move)
Assert.True(_s.DoesAliceWin("hello")); // e,o = 2 vowels (even), but Alice still wins
Assert.True(_s.DoesAliceWin("beautiful")); // e,a,u,i,u = 5 vowels (odd)
// If total vowels = even > 0: Alice wins (leaves exactly 1 vowel for Bob)
Assert.True(_s.DoesAliceWin("code")); // o,e = 2 vowels (even)
Assert.True(_s.DoesAliceWin("education")); // e,u,a,i,o = 5 vowels, but any vowels = Alice wins
}
[Theory]
[InlineData("", false)] // empty string - no vowels
[InlineData("aeiou", true)] // all vowels
[InlineData("bcdfg", false)] // all consonants
[InlineData("hello", true)] // mixed with vowels
[InlineData("rhythm", false)] // no vowels (y not considered vowel)
[InlineData("queue", true)] // multiple same vowels
public void ParameterizedTests(string input, bool expected)
{
// Act & Assert
Assert.Equal(expected, _s.DoesAliceWin(input));
}
}

View File

@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../csharp/csharp.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,20 @@
# 邊界情況清單3227 Vowels Game In A String
## 需要測試的邊界
- [ ] 空輸入 / 單一元素
- [ ] 重複元素 / 全相同
- [ ] 極值(最小/最大)
- [ ] 含負數 / 0 / 大數
- [ ] 大資料量(接近上限)
## 額外案例
### 案例 1
- 輸入:
- 預期:
- 說明:
### 案例 2
- 輸入:
- 預期:
- 說明:

View File

@@ -0,0 +1,188 @@
// LeetCode 3227: Vowels Game in a String 單元測試Go testing
// Problem: Alice and Bob play a game. Alice removes substrings with odd vowels,
// Bob removes substrings with even vowels. Alice wins if string has any vowels.
package vowels_test
import (
"fmt"
"testing"
vowels "leetcode/3227-vowels-game-in-a-string/go"
)
func TestExample1(t *testing.T) {
// "leetcode" contains vowels: e,e,o,e
input := "leetcode"
got := vowels.DoesAliceWin(input)
want := true // Alice wins
if got != want {
t.Fatalf("input %q: want %v got %v", input, want, got)
}
}
func TestExample2(t *testing.T) {
// "bcdf" has no vowels
input := "bcdf"
got := vowels.DoesAliceWin(input)
want := false // Bob wins (Alice can't make first move)
if got != want {
t.Fatalf("input %q: want %v got %v", input, want, got)
}
}
func TestAllVowels(t *testing.T) {
// All vowels string - Alice removes entire string
input := "aeiou"
got := vowels.DoesAliceWin(input)
want := true
if got != want {
t.Fatalf("input %q: want %v got %v", input, want, got)
}
}
func TestSingleVowel(t *testing.T) {
// Single vowel - Alice removes it immediately
input := "a"
got := vowels.DoesAliceWin(input)
want := true
if got != want {
t.Fatalf("input %q: want %v got %v", input, want, got)
}
}
func TestSingleConsonant(t *testing.T) {
// Single consonant - Alice can't move
input := "b"
got := vowels.DoesAliceWin(input)
want := false
if got != want {
t.Fatalf("input %q: want %v got %v", input, want, got)
}
}
func TestMixedString(t *testing.T) {
// "programming" has vowels: o,a,i
input := "programming"
got := vowels.DoesAliceWin(input)
want := true // Alice can remove vowels
if got != want {
t.Fatalf("input %q: want %v got %v", input, want, got)
}
}
func TestLongStringNoVowels(t *testing.T) {
// All consonants - no vowels means Bob wins
input := "bcdfghjklmnpqrstvwxyz"
got := vowels.DoesAliceWin(input)
want := false
if got != want {
t.Fatalf("input %q: want %v got %v", input, want, got)
}
}
func TestRepeatedVowels(t *testing.T) {
// Even number of same vowels - Alice can leave 1 for Bob
input := "aaaa"
got := vowels.DoesAliceWin(input)
want := true
if got != want {
t.Fatalf("input %q: want %v got %v", input, want, got)
}
}
func TestEdgeCases(t *testing.T) {
testCases := []struct {
input string
want bool
desc string
}{
{"e", true, "single vowel"},
{"x", false, "single consonant"},
{"ab", true, "has vowel 'a'"},
{"xy", false, "no vowels"},
{"xabc", true, "vowel in middle"},
{"abcx", true, "vowel at start"},
{"xbca", true, "vowel at end"},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
got := vowels.DoesAliceWin(tc.input)
if got != tc.want {
t.Fatalf("input %q (%s): want %v got %v", tc.input, tc.desc, tc.want, got)
}
})
}
}
func TestGameTheoryValidation(t *testing.T) {
testCases := []struct {
input string
want bool
desc string
}{
// If total vowels = 0: Alice loses
{"bcdfg", false, "no vowels - Alice can't move"},
{"rhythm", false, "no vowels (y not counted)"},
// Any vowels > 0: Alice wins
{"hello", true, "e,o vowels - Alice wins"},
{"beautiful", true, "e,a,u,i,u vowels - Alice wins"},
{"code", true, "o,e vowels - Alice wins"},
{"education", true, "e,u,a,i,o vowels - Alice wins"},
{"queue", true, "u,e,u,e vowels - Alice wins"},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
got := vowels.DoesAliceWin(tc.input)
if got != tc.want {
t.Fatalf("input %q (%s): want %v got %v", tc.input, tc.desc, tc.want, got)
}
})
}
}
func TestParameterized(t *testing.T) {
testCases := []struct {
input string
want bool
}{
{"", false}, // empty string
{"aeiou", true}, // all vowels
{"bcdfg", false}, // all consonants
{"hello", true}, // mixed with vowels
{"rhythm", false}, // no vowels
{"queue", true}, // multiple same vowels
{"xyz", false}, // short no vowels
{"programming", true}, // longer mixed
}
for _, tc := range testCases {
t.Run(tc.input, func(t *testing.T) {
got := vowels.DoesAliceWin(tc.input)
if got != tc.want {
t.Errorf("doesAliceWin(%q) = %v, want %v", tc.input, got, tc.want)
}
})
}
}
// Benchmark test
func BenchmarkDoesAliceWin(b *testing.B) {
testString := "abcdefghijklmnopqrstuvwxyz"
b.ResetTimer()
for i := 0; i < b.N; i++ {
vowels.DoesAliceWin(testString)
}
}
// Example test for documentation
func ExampleDoesAliceWin() {
result1 := vowels.DoesAliceWin("leetcode")
result2 := vowels.DoesAliceWin("bcdf")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}