diff --git a/Algorithms.Tests/Strings/ValidParenthesesTests.cs b/Algorithms.Tests/Strings/ValidParenthesesTests.cs new file mode 100644 index 00000000..c376032e --- /dev/null +++ b/Algorithms.Tests/Strings/ValidParenthesesTests.cs @@ -0,0 +1,70 @@ +using System; +using NUnit.Framework; +using Algorithms.Strings; + +namespace Algorithms.Tests.Strings +{ + public static class ValidParenthesesTests + { + [TestCase("([{}])")] + [TestCase("((({{{[[[]]]}}})))")] + public static void IsValidParentheses_TrueExpected(string parentheses) + { + // Arrange + // Act + var isValidParentheses = ValidParentheses.IsValidParentheses(parentheses); + + // Assert + Assert.That(isValidParentheses, Is.True); + } + + [TestCase("([)[}{")] + [TestCase("([}}])")] + public static void IsValidParentheses_FalseExpected(string parentheses) + { + // Arrange + // Act + var isValidParentheses = ValidParentheses.IsValidParentheses(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 new file mode 100644 index 00000000..a71e1686 --- /dev/null +++ b/Algorithms/Strings/ValidParentheses.cs @@ -0,0 +1,54 @@ +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 static 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; + } + + Dictionary bracketPairs = new Dictionary + { + { ')', '(' }, + { '}', '{' }, + { ']', '[' }, + }; + + Stack stack = new Stack(); + + foreach (char c in parentheses) + { + if (bracketPairs.ContainsValue(c)) + { + stack.Push(c); + } + else if (bracketPairs.ContainsKey(c)) + { + if (stack.Count == 0 || stack.Pop() != bracketPairs[c]) + { + return false; + } + } + else + { + return false; + } + } + + 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)