File tree Expand file tree Collapse file tree 2 files changed +59
-34
lines changed Expand file tree Collapse file tree 2 files changed +59
-34
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ namespace Algorithms.Tests.Strings
7
7
public static class ValidParenthesesTests
8
8
{
9
9
[ TestCase ( "([{}])" ) ]
10
+ [ TestCase ( "((({{{[[[]]]}}})))" ) ]
10
11
public static void IsValidParentheses_TrueExpected ( string parentheses )
11
12
{
12
13
// Arrange
@@ -17,7 +18,7 @@ public static void IsValidParentheses_TrueExpected(string parentheses)
17
18
Assert . That ( isValidParentheses , Is . True ) ;
18
19
}
19
20
20
- [ TestCase ( "([)[}" ) ]
21
+ [ TestCase ( "([)[}{ " ) ]
21
22
[ TestCase ( "([}}])" ) ]
22
23
public static void IsValidParentheses_FalseExpected ( string parentheses )
23
24
{
@@ -28,5 +29,42 @@ public static void IsValidParentheses_FalseExpected(string parentheses)
28
29
// Assert
29
30
Assert . That ( isValidParentheses , Is . False ) ;
30
31
}
32
+
33
+ [ TestCase ( "(" ) ]
34
+ [ TestCase ( "(((" ) ]
35
+ [ TestCase ( "({{}" ) ]
36
+ public static void IsValidParentheses_OddLength ( string parentheses )
37
+ {
38
+ // Arrange
39
+ // Act
40
+ var isValidParentheses = ValidParentheses . IsValidParentheses ( parentheses ) ;
41
+
42
+ // Assert
43
+ Assert . That ( isValidParentheses , Is . False ) ;
44
+ }
45
+
46
+ [ TestCase ( "a" ) ]
47
+ [ TestCase ( "[a]" ) ]
48
+ [ TestCase ( "//" ) ]
49
+ public static void IsValidParentheses_InvalidCharFalse ( string parentheses )
50
+ {
51
+ // Arrange
52
+ // Act
53
+ var isValidParentheses = ValidParentheses . IsValidParentheses ( parentheses ) ;
54
+
55
+ // Assert
56
+ Assert . That ( isValidParentheses , Is . False ) ;
57
+ }
58
+
59
+ [ Test ]
60
+ public static void IsValidParentheses_EmptyStringTrue ( )
61
+ {
62
+ // Arrange
63
+ // Act
64
+ var isValidParentheses = ValidParentheses . IsValidParentheses ( string . Empty ) ;
65
+
66
+ // Assert
67
+ Assert . That ( isValidParentheses , Is . True ) ;
68
+ }
31
69
}
32
70
}
Original file line number Diff line number Diff line change @@ -20,44 +20,31 @@ public static bool IsValidParentheses(string parentheses)
20
20
return false ;
21
21
}
22
22
23
+ Dictionary < char , char > bracketPairs = new Dictionary < char , char >
24
+ {
25
+ { ')' , '(' } ,
26
+ { '}' , '{' } ,
27
+ { ']' , '[' } ,
28
+ } ;
29
+
23
30
Stack < char > stack = new Stack < char > ( ) ;
24
31
25
- foreach ( char c in parentheses )
32
+ foreach ( char c in parentheses )
26
33
{
27
- switch ( c )
34
+ if ( bracketPairs . ContainsValue ( c ) )
28
35
{
29
- case '(' :
30
- case '{' :
31
- case '[' :
32
- stack . Push ( c ) ;
33
- break ;
34
-
35
- case ')' :
36
- if ( stack . Count == 0 || stack . Pop ( ) != '(' )
37
- {
38
- return false ;
39
- }
40
-
41
- break ;
42
-
43
- case '}' :
44
- if ( stack . Count == 0 || stack . Pop ( ) != '{' )
45
- {
46
- return false ;
47
- }
48
-
49
- break ;
50
-
51
- case ']' :
52
- if ( stack . Count == 0 || stack . Pop ( ) != '[' )
53
- {
54
- return false ;
55
- }
56
-
57
- break ;
58
-
59
- default :
36
+ stack . Push ( c ) ;
37
+ }
38
+ else if ( bracketPairs . ContainsKey ( c ) )
39
+ {
40
+ if ( stack . Count == 0 || stack . Pop ( ) != bracketPairs [ c ] )
41
+ {
60
42
return false ;
43
+ }
44
+ }
45
+ else
46
+ {
47
+ return false ;
61
48
}
62
49
}
63
50
You can’t perform that action at this time.
0 commit comments