Skip to content

Add Valid Parentheses Checker algorithm #510

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 1 commit 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
31 changes: 31 additions & 0 deletions Algorithms.Tests/Strings/ValidParenthesesTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
52 changes: 52 additions & 0 deletions Algorithms/Strings/ValidParentheses.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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 class ValidParentheses

Check warning on line 10 in Algorithms/Strings/ValidParentheses.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Algorithms/Strings/ValidParentheses.cs#L10

Add a 'protected' constructor or the 'static' keyword to the class declaration.
{
/// <summary>
/// Function to check if the parentheses is valid.
/// </summary>
/// <param name="parentheses">String to be checked.</param>
public static bool IsValidParentheses(string parentheses)

Check failure on line 16 in Algorithms/Strings/ValidParentheses.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Algorithms/Strings/ValidParentheses.cs#L16

The Cyclomatic Complexity of this method is 15 which is greater than 10 authorized.
{
if (parentheses.Length % 2 != 0)
{
return false;
}

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

foreach(char c in parentheses.ToCharArray())

Check notice on line 25 in Algorithms/Strings/ValidParentheses.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Algorithms/Strings/ValidParentheses.cs#L25

Remove this redundant 'ToCharArray' call.
{
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);
}

Check warning on line 46 in Algorithms/Strings/ValidParentheses.cs

View check run for this annotation

Codecov / codecov/patch

Algorithms/Strings/ValidParentheses.cs#L44-L46

Added lines #L44 - L46 were not covered by tests
}

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