-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParanthesisProblem.cs
More file actions
55 lines (46 loc) · 1.48 KB
/
Copy pathParanthesisProblem.cs
File metadata and controls
55 lines (46 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
namespace Stacks
{
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class ParanthesisProblem
{
public bool ValidateParanthesis(char[] bracketsList)
{
if (bracketsList == null || bracketsList.Length == 0) return false;
Stack<char> stack = new Stack<char>();
for(int i=0; i <= bracketsList.Length - 1; i++)
{
if (bracketsList[i] == '{' || bracketsList[i] == '(')
{
stack.Push(bracketsList[i]);
}
else
{
char openbrackets = stack.Pop();
if (!this.IsValidMatchingbrackets(openbrackets, bracketsList[i]))
{
return false;
}
}
}
if (stack.Count > 0) return false;
return true;
}
private bool IsValidMatchingbrackets(char open, char close)
{
if (open == '(' && close == ')')
return true;
else if (open == '{' && close == '}')
return true;
return false;
}
[TestMethod]
public void TestValidateParanthesis()
{
string paranthesis = "{{";
bool result = this.ValidateParanthesis(paranthesis.ToCharArray());
Assert.AreEqual(result, false);
}
}
}