Skip to content

Add Valid Parentheses Checker algorithm #512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions Algorithms.Tests/Strings/ValidParenthesesTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
54 changes: 54 additions & 0 deletions Algorithms/Strings/ValidParentheses.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;

namespace Algorithms.Strings
{
/// <summary>
/// This is a class for checking if the parentheses is valid.
/// A valid parentheses should have opening brace and closing brace.
/// </summary>
public static class ValidParentheses
{
/// <summary>
/// Function to check if the parentheses is valid.
/// </summary>
/// <param name="parentheses">String to be checked.</param>
public static bool IsValidParentheses(string parentheses)
{
if (parentheses.Length % 2 != 0)
{
return false;
}

Dictionary<char, char> bracketPairs = new Dictionary<char, char>
{
{ ')', '(' },
{ '}', '{' },
{ ']', '[' },
};

Stack<char> stack = new Stack<char>();

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;
}
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down