Skip to content

Commit bc160b4

Browse files
committed
Add Valid Parentheses Checker algorithm
1 parent 8fec042 commit bc160b4

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using NUnit.Framework;
3+
using Algorithms.Strings;
4+
5+
namespace Algorithms.Tests.Strings
6+
{
7+
public static class ValidParenthesesTests
8+
{
9+
[TestCase("([{}])")]
10+
public static void IsValidParentheses_TrueExpected(string parentheses)
11+
{
12+
// Arrange
13+
// Act
14+
var isValidParentheses = ValidParentheses.IsValidParentheses(parentheses);
15+
16+
// Assert
17+
Assert.That(isValidParentheses, Is.True);
18+
}
19+
20+
[TestCase("([)[}")]
21+
public static void IsValidParentheses_FalseExpected(string parentheses)
22+
{
23+
// Arrange
24+
// Act
25+
var isValidParentheses = ValidParentheses.IsValidParentheses(parentheses);
26+
27+
// Assert
28+
Assert.That(isValidParentheses, Is.False);
29+
}
30+
}
31+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Algorithms.Strings
5+
{
6+
/// <summary>
7+
/// This is a class for checking if the parentheses is valid.
8+
/// A valid parentheses should have opening brace and closing brace.
9+
/// </summary>
10+
public class ValidParentheses
11+
{
12+
/// <summary>
13+
/// Function to check if the parentheses is valid.
14+
/// </summary>
15+
/// <param name="parentheses">String to be checked.</param>
16+
public static bool IsValidParentheses(string parentheses)
17+
{
18+
if (parentheses.Length % 2 != 0)
19+
{
20+
return false;
21+
}
22+
23+
Stack<char> stack = new Stack<char>();
24+
25+
foreach(char c in parentheses.ToCharArray())
26+
{
27+
if(c == '(' || c == '{' || c == '[')
28+
{
29+
stack.Push(c);
30+
}
31+
else if (c == ')' && stack.Count != 0 && stack.Peek() == '(')
32+
{
33+
stack.Pop();
34+
}
35+
else if (c == '}' && stack.Count != 0 && stack.Peek() == '{')
36+
{
37+
stack.Pop();
38+
}
39+
else if (c == ']' && stack.Count != 0 && stack.Peek() == '[')
40+
{
41+
stack.Pop();
42+
}
43+
else
44+
{
45+
stack.Push(c);
46+
}
47+
}
48+
49+
return stack.Count == 0;
50+
}
51+
}
52+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ find more than one implementation for the same objective but using different alg
213213
* [Longest Consecutive Character](./Algorithms/Strings/GeneralStringAlgorithms.cs)
214214
* [Palindrome Checker](./Algorithms/Strings/Palindrome.cs)
215215
* [Get all permutations of a string](./Algorithms/Strings/Permutation.cs)
216+
* [Valid Parentheses Checker](./Algorithms/Strings/ValidParentheses.cs)
216217
* [Other](./Algorithms/Other)
217218
* [Fermat Prime Checker](./Algorithms/Other/FermatPrimeChecker.cs)
218219
* [Sieve of Eratosthenes](./Algorithms/Other/SieveOfEratosthenes.cs)

0 commit comments

Comments
 (0)