Skip to content

Commit abf73c9

Browse files
committed
Added additional test cases for ValidParenthesesTests.cs
Refactored switch-case to dictionary in ValidParentheses.cs
1 parent a2c5d5d commit abf73c9

File tree

2 files changed

+59
-34
lines changed

2 files changed

+59
-34
lines changed

Algorithms.Tests/Strings/ValidParenthesesTests.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Algorithms.Tests.Strings
77
public static class ValidParenthesesTests
88
{
99
[TestCase("([{}])")]
10+
[TestCase("((({{{[[[]]]}}})))")]
1011
public static void IsValidParentheses_TrueExpected(string parentheses)
1112
{
1213
// Arrange
@@ -17,7 +18,7 @@ public static void IsValidParentheses_TrueExpected(string parentheses)
1718
Assert.That(isValidParentheses, Is.True);
1819
}
1920

20-
[TestCase("([)[}")]
21+
[TestCase("([)[}{")]
2122
[TestCase("([}}])")]
2223
public static void IsValidParentheses_FalseExpected(string parentheses)
2324
{
@@ -28,5 +29,42 @@ public static void IsValidParentheses_FalseExpected(string parentheses)
2829
// Assert
2930
Assert.That(isValidParentheses, Is.False);
3031
}
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+
}
3169
}
3270
}

Algorithms/Strings/ValidParentheses.cs

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,31 @@ public static bool IsValidParentheses(string parentheses)
2020
return false;
2121
}
2222

23+
Dictionary<char, char> bracketPairs = new Dictionary<char, char>
24+
{
25+
{ ')', '(' },
26+
{ '}', '{' },
27+
{ ']', '[' },
28+
};
29+
2330
Stack<char> stack = new Stack<char>();
2431

25-
foreach(char c in parentheses)
32+
foreach (char c in parentheses)
2633
{
27-
switch (c)
34+
if (bracketPairs.ContainsValue(c))
2835
{
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+
{
6042
return false;
43+
}
44+
}
45+
else
46+
{
47+
return false;
6148
}
6249
}
6350

0 commit comments

Comments
 (0)