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] 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)