Skip to content

Commit 6b079c4

Browse files
committed
Merge remote-tracking branch 'origin/main'
# Conflicts: # LeetCodePatterns.sln
2 parents 1d23d5b + fa636db commit 6b079c4

32 files changed

+548
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,3 +723,4 @@ FodyWeavers.xsd
723723

724724
# End of https://www.toptal.com/developers/gitignore/api/csharp
725725
/.dockerignore
726+
/.config/dotnet-tools.json
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace DailyLeetCode.Test;
2+
3+
public class MakeSquareSameColorTests
4+
{
5+
[Theory]
6+
[MemberData(nameof(TestData))]
7+
public void CanMakeSquare_AllShouldPass(char[][] grid, bool expected)
8+
{
9+
var actual = MakeSquareSameColor.CanMakeSquare(grid);
10+
Assert.Equal(expected, actual);
11+
}
12+
13+
14+
15+
public static IEnumerable<object[]> TestData()
16+
{
17+
yield return
18+
[
19+
new char[][] { new char[] { 'B', 'W', 'B' }, new char[] { 'B', 'W', 'W' }, new char[] { 'B', 'W', 'B' } }
20+
, true
21+
];
22+
yield return
23+
[
24+
new char[][] { new char[] { 'B', 'W', 'B' }, new char[] { 'W', 'B', 'W' }, new char[] { 'B', 'W', 'B' } },
25+
false
26+
];
27+
28+
yield return
29+
[
30+
new char[][] { new char[] { 'B', 'B', 'B' }, new char[] { 'B', 'W', 'W' }, new char[] { 'W', 'B', 'B' } },
31+
true
32+
];
33+
}
34+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace DailyLeetCode.Test;
2+
3+
public class MinimumRemoveMakeValidParenthesesTests
4+
{
5+
[Theory]
6+
[InlineData("lee(t(c)o)de)", "lee(t(c)o)de")]
7+
[InlineData("a)b(c)d", "ab(c)d")]
8+
[InlineData("))((", "")]
9+
public void Test(string input, string expected)
10+
{
11+
var actual = MinimumRemoveMakeValidParentheses.MinRemoveToMakeValid(input);
12+
Assert.Equal(expected, actual);
13+
}
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace DailyLeetCode.Test;
2+
3+
public class ValidParenthesisStringTests
4+
{
5+
[Theory]
6+
[InlineData("()", true)]
7+
[InlineData("()*", true)]
8+
[InlineData("(*)", true)]
9+
[InlineData("(*))", true)]
10+
[InlineData("((*)", true)]
11+
[InlineData("((*))", true)]
12+
[InlineData("((*)))", true)]
13+
[InlineData("((*)))", true)]
14+
[InlineData("((*)))", true)]
15+
public void Test(string s, bool expected)
16+
{
17+
var actual = ValidParenthesisString.CheckValidString(s);
18+
Assert.Equal(expected, actual);
19+
}
20+
}

DailyLeetCode/MakeSquareSameColor.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace DailyLeetCode;
2+
3+
public class MakeSquareSameColor
4+
{
5+
/// <summary>
6+
/// You are given a 2D matrix grid of size 3 x 3 consisting only of characters 'B' and 'W'. Character 'W' represents the white color, and character 'B' represents the black color.
7+
8+
//Your task is to change the color of at most one cell so that the matrix has a 2 x 2 square where all cells are of the same color.
9+
//Return true if it is possible to create a 2 x 2 square of the same color, otherwise, return false.
10+
/// </summary>
11+
/// <param name="grid"></param>
12+
/// <returns></returns>
13+
public static bool CanMakeSquare(char[][] grid)
14+
{
15+
return false;
16+
}
17+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Text;
2+
namespace DailyLeetCode;
3+
4+
public class MinimumRemoveMakeValidParentheses
5+
{
6+
public static string MinRemoveToMakeValid(string s)
7+
{
8+
var stack = new Dictionary<char, Stack<int>>();
9+
for (var i = 0; i < s.Length; i++)
10+
{
11+
if (s[i] == '(')
12+
{
13+
if (!stack.ContainsKey(s[i]))
14+
stack[s[i]] = new Stack<int>();
15+
stack[s[i]].Push(i);
16+
}
17+
else if (s[i] == ')')
18+
{
19+
if (stack.ContainsKey('('))
20+
{
21+
stack['('].Pop();
22+
if (stack['('].Count < 1)
23+
stack.Remove('(');
24+
}
25+
else
26+
{
27+
s = s.Remove(i, 1);
28+
i--;
29+
}
30+
}
31+
}
32+
33+
stack.TryGetValue('(', out var opens);
34+
while (opens?.Count > 0)
35+
s = s.Remove(opens.Pop(), 1);
36+
return s;
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace DailyLeetCode;
2+
3+
public class ValidParenthesisString
4+
{
5+
/// <summary>
6+
/// Given a string s containing only three types of characters: '(', ')' and '*', return true if s is valid.
7+
/// The following rules define a valid string:
8+
/// Any left parenthesis '(' must have a corresponding right parenthesis ')'.
9+
/// Any right parenthesis ')' must have a corresponding left parenthesis '('.
10+
/// Left parenthesis '(' must go before the corresponding right parenthesis ')'.
11+
/// '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string "".
12+
/// </summary>
13+
/// <param name="s"></param>
14+
/// <returns></returns>
15+
public static bool CheckValidString(string s)
16+
{
17+
var stack = new Stack<char>();
18+
var starStack = new Stack<char>();
19+
foreach (var c in s)
20+
{
21+
if (c == '(') stack.Push(c);
22+
else if (c == '*') starStack.Push(c);
23+
else
24+
{
25+
if (stack.Count > 0) stack.Pop();
26+
else if (starStack.Count > 0) starStack.Pop();
27+
else return false;
28+
}
29+
}
30+
while (stack.Count > 0 && starStack.Count > 0)
31+
{
32+
if (stack.Pop() != starStack.Pop()) return false;
33+
}
34+
35+
if (stack.Count > 0) return false;
36+
return stack.Count == 0;
37+
}
38+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace InPlaceReverseLinkedList;
2+
3+
public class Class1 { }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
15+
<PackageReference Include="xunit" Version="2.5.3"/>
16+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<Using Include="Xunit"/>
21+
</ItemGroup>
22+
23+
</Project>

0 commit comments

Comments
 (0)