From bc160b43afd0aeeff9fa5daa874e9966bbf667a3 Mon Sep 17 00:00:00 2001
From: Jc Calayag <138150245+Jccqt@users.noreply.github.com>
Date: Sat, 12 Jul 2025 06:49:08 +0800
Subject: [PATCH 1/3] Add Valid Parentheses Checker algorithm
---
.../Strings/ValidParenthesesTests.cs | 31 +++++++++++
Algorithms/Strings/ValidParentheses.cs | 52 +++++++++++++++++++
README.md | 1 +
3 files changed, 84 insertions(+)
create mode 100644 Algorithms.Tests/Strings/ValidParenthesesTests.cs
create mode 100644 Algorithms/Strings/ValidParentheses.cs
diff --git a/Algorithms.Tests/Strings/ValidParenthesesTests.cs b/Algorithms.Tests/Strings/ValidParenthesesTests.cs
new file mode 100644
index 00000000..420754fb
--- /dev/null
+++ b/Algorithms.Tests/Strings/ValidParenthesesTests.cs
@@ -0,0 +1,31 @@
+using System;
+using NUnit.Framework;
+using Algorithms.Strings;
+
+namespace Algorithms.Tests.Strings
+{
+ public static class ValidParenthesesTests
+ {
+ [TestCase("([{}])")]
+ public static void IsValidParentheses_TrueExpected(string parentheses)
+ {
+ // Arrange
+ // Act
+ var isValidParentheses = ValidParentheses.IsValidParentheses(parentheses);
+
+ // Assert
+ Assert.That(isValidParentheses, Is.True);
+ }
+
+ [TestCase("([)[}")]
+ public static void IsValidParentheses_FalseExpected(string parentheses)
+ {
+ // Arrange
+ // Act
+ var isValidParentheses = ValidParentheses.IsValidParentheses(parentheses);
+
+ // Assert
+ Assert.That(isValidParentheses, Is.False);
+ }
+ }
+}
diff --git a/Algorithms/Strings/ValidParentheses.cs b/Algorithms/Strings/ValidParentheses.cs
new file mode 100644
index 00000000..b5616343
--- /dev/null
+++ b/Algorithms/Strings/ValidParentheses.cs
@@ -0,0 +1,52 @@
+using System;
+using System.Collections.Generic;
+
+namespace Algorithms.Strings
+{
+ ///
+ /// This is a class for checking if the parentheses is valid.
+ /// A valid parentheses should have opening brace and closing brace.
+ ///
+ public class ValidParentheses
+ {
+ ///
+ /// Function to check if the parentheses is valid.
+ ///
+ /// String to be checked.
+ public static bool IsValidParentheses(string parentheses)
+ {
+ if (parentheses.Length % 2 != 0)
+ {
+ return false;
+ }
+
+ Stack stack = new Stack();
+
+ foreach(char c in parentheses.ToCharArray())
+ {
+ if(c == '(' || c == '{' || c == '[')
+ {
+ stack.Push(c);
+ }
+ else if (c == ')' && stack.Count != 0 && stack.Peek() == '(')
+ {
+ stack.Pop();
+ }
+ else if (c == '}' && stack.Count != 0 && stack.Peek() == '{')
+ {
+ stack.Pop();
+ }
+ else if (c == ']' && stack.Count != 0 && stack.Peek() == '[')
+ {
+ stack.Pop();
+ }
+ else
+ {
+ stack.Push(c);
+ }
+ }
+
+ return stack.Count == 0;
+ }
+ }
+}
diff --git a/README.md b/README.md
index b9933f9f..d57f3446 100644
--- a/README.md
+++ b/README.md
@@ -213,6 +213,7 @@ find more than one implementation for the same objective but using different alg
* [Longest Consecutive Character](./Algorithms/Strings/GeneralStringAlgorithms.cs)
* [Palindrome Checker](./Algorithms/Strings/Palindrome.cs)
* [Get all permutations of a string](./Algorithms/Strings/Permutation.cs)
+ * [Valid Parentheses Checker](./Algorithms/Strings/ValidParentheses.cs)
* [Other](./Algorithms/Other)
* [Fermat Prime Checker](./Algorithms/Other/FermatPrimeChecker.cs)
* [Sieve of Eratosthenes](./Algorithms/Other/SieveOfEratosthenes.cs)
From a2c5d5d262e9d2a153cba97c83ce3cfb381cdacb Mon Sep 17 00:00:00 2001
From: Jc Calayag <138150245+Jccqt@users.noreply.github.com>
Date: Sat, 12 Jul 2025 07:07:51 +0800
Subject: [PATCH 2/3] Added more test cases for ValidParenthesesTests.cs
Refactored nested if-else to switch-case in ValidParentheses.cs
---
.../Strings/ValidParenthesesTests.cs | 1 +
Algorithms/Strings/ValidParentheses.cs | 55 ++++++++++++-------
2 files changed, 36 insertions(+), 20 deletions(-)
diff --git a/Algorithms.Tests/Strings/ValidParenthesesTests.cs b/Algorithms.Tests/Strings/ValidParenthesesTests.cs
index 420754fb..66ca55c3 100644
--- a/Algorithms.Tests/Strings/ValidParenthesesTests.cs
+++ b/Algorithms.Tests/Strings/ValidParenthesesTests.cs
@@ -18,6 +18,7 @@ public static void IsValidParentheses_TrueExpected(string parentheses)
}
[TestCase("([)[}")]
+ [TestCase("([}}])")]
public static void IsValidParentheses_FalseExpected(string parentheses)
{
// Arrange
diff --git a/Algorithms/Strings/ValidParentheses.cs b/Algorithms/Strings/ValidParentheses.cs
index b5616343..4ccd58d9 100644
--- a/Algorithms/Strings/ValidParentheses.cs
+++ b/Algorithms/Strings/ValidParentheses.cs
@@ -7,7 +7,7 @@ namespace Algorithms.Strings
/// This is a class for checking if the parentheses is valid.
/// A valid parentheses should have opening brace and closing brace.
///
- public class ValidParentheses
+ public static class ValidParentheses
{
///
/// Function to check if the parentheses is valid.
@@ -22,27 +22,42 @@ public static bool IsValidParentheses(string parentheses)
Stack stack = new Stack();
- foreach(char c in parentheses.ToCharArray())
+ foreach(char c in parentheses)
{
- if(c == '(' || c == '{' || c == '[')
+ switch (c)
{
- stack.Push(c);
- }
- else if (c == ')' && stack.Count != 0 && stack.Peek() == '(')
- {
- stack.Pop();
- }
- else if (c == '}' && stack.Count != 0 && stack.Peek() == '{')
- {
- stack.Pop();
- }
- else if (c == ']' && stack.Count != 0 && stack.Peek() == '[')
- {
- stack.Pop();
- }
- else
- {
- stack.Push(c);
+ case '(':
+ case '{':
+ case '[':
+ stack.Push(c);
+ break;
+
+ case ')':
+ if (stack.Count == 0 || stack.Pop() != '(')
+ {
+ return false;
+ }
+
+ break;
+
+ case '}':
+ if (stack.Count == 0 || stack.Pop() != '{')
+ {
+ return false;
+ }
+
+ break;
+
+ case ']':
+ if (stack.Count == 0 || stack.Pop() != '[')
+ {
+ return false;
+ }
+
+ break;
+
+ default:
+ return false;
}
}
From abf73c9de8c7586399b3e400c2b1cebcf97c240f Mon Sep 17 00:00:00 2001
From: Jc Calayag <138150245+Jccqt@users.noreply.github.com>
Date: Sat, 12 Jul 2025 07:27:14 +0800
Subject: [PATCH 3/3] Added additional test cases for ValidParenthesesTests.cs
Refactored switch-case to dictionary in ValidParentheses.cs
---
.../Strings/ValidParenthesesTests.cs | 40 +++++++++++++-
Algorithms/Strings/ValidParentheses.cs | 53 +++++++------------
2 files changed, 59 insertions(+), 34 deletions(-)
diff --git a/Algorithms.Tests/Strings/ValidParenthesesTests.cs b/Algorithms.Tests/Strings/ValidParenthesesTests.cs
index 66ca55c3..c376032e 100644
--- a/Algorithms.Tests/Strings/ValidParenthesesTests.cs
+++ b/Algorithms.Tests/Strings/ValidParenthesesTests.cs
@@ -7,6 +7,7 @@ namespace Algorithms.Tests.Strings
public static class ValidParenthesesTests
{
[TestCase("([{}])")]
+ [TestCase("((({{{[[[]]]}}})))")]
public static void IsValidParentheses_TrueExpected(string parentheses)
{
// Arrange
@@ -17,7 +18,7 @@ public static void IsValidParentheses_TrueExpected(string parentheses)
Assert.That(isValidParentheses, Is.True);
}
- [TestCase("([)[}")]
+ [TestCase("([)[}{")]
[TestCase("([}}])")]
public static void IsValidParentheses_FalseExpected(string parentheses)
{
@@ -28,5 +29,42 @@ public static void IsValidParentheses_FalseExpected(string parentheses)
// Assert
Assert.That(isValidParentheses, Is.False);
}
+
+ [TestCase("(")]
+ [TestCase("(((")]
+ [TestCase("({{}")]
+ public static void IsValidParentheses_OddLength(string parentheses)
+ {
+ // Arrange
+ // Act
+ var isValidParentheses = ValidParentheses.IsValidParentheses(parentheses);
+
+ // Assert
+ Assert.That(isValidParentheses, Is.False);
+ }
+
+ [TestCase("a")]
+ [TestCase("[a]")]
+ [TestCase("//")]
+ public static void IsValidParentheses_InvalidCharFalse(string parentheses)
+ {
+ // Arrange
+ // Act
+ var isValidParentheses = ValidParentheses.IsValidParentheses(parentheses);
+
+ // Assert
+ Assert.That(isValidParentheses, Is.False);
+ }
+
+ [Test]
+ public static void IsValidParentheses_EmptyStringTrue()
+ {
+ // Arrange
+ // Act
+ var isValidParentheses = ValidParentheses.IsValidParentheses(string.Empty);
+
+ // Assert
+ Assert.That(isValidParentheses, Is.True);
+ }
}
}
diff --git a/Algorithms/Strings/ValidParentheses.cs b/Algorithms/Strings/ValidParentheses.cs
index 4ccd58d9..a71e1686 100644
--- a/Algorithms/Strings/ValidParentheses.cs
+++ b/Algorithms/Strings/ValidParentheses.cs
@@ -20,44 +20,31 @@ public static bool IsValidParentheses(string parentheses)
return false;
}
+ Dictionary bracketPairs = new Dictionary
+ {
+ { ')', '(' },
+ { '}', '{' },
+ { ']', '[' },
+ };
+
Stack stack = new Stack();
- foreach(char c in parentheses)
+ foreach (char c in parentheses)
{
- switch (c)
+ if (bracketPairs.ContainsValue(c))
{
- case '(':
- case '{':
- case '[':
- stack.Push(c);
- break;
-
- case ')':
- if (stack.Count == 0 || stack.Pop() != '(')
- {
- return false;
- }
-
- break;
-
- case '}':
- if (stack.Count == 0 || stack.Pop() != '{')
- {
- return false;
- }
-
- break;
-
- case ']':
- if (stack.Count == 0 || stack.Pop() != '[')
- {
- return false;
- }
-
- break;
-
- default:
+ stack.Push(c);
+ }
+ else if (bracketPairs.ContainsKey(c))
+ {
+ if (stack.Count == 0 || stack.Pop() != bracketPairs[c])
+ {
return false;
+ }
+ }
+ else
+ {
+ return false;
}
}