File tree Expand file tree Collapse file tree 3 files changed +84
-0
lines changed Expand file tree Collapse file tree 3 files changed +84
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -213,6 +213,7 @@ find more than one implementation for the same objective but using different alg
213
213
* [ Longest Consecutive Character] ( ./Algorithms/Strings/GeneralStringAlgorithms.cs )
214
214
* [ Palindrome Checker] ( ./Algorithms/Strings/Palindrome.cs )
215
215
* [ Get all permutations of a string] ( ./Algorithms/Strings/Permutation.cs )
216
+ * [ Valid Parentheses Checker] ( ./Algorithms/Strings/ValidParentheses.cs )
216
217
* [ Other] ( ./Algorithms/Other )
217
218
* [ Fermat Prime Checker] ( ./Algorithms/Other/FermatPrimeChecker.cs )
218
219
* [ Sieve of Eratosthenes] ( ./Algorithms/Other/SieveOfEratosthenes.cs )
You can’t perform that action at this time.
0 commit comments