-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidParentheses.cs
More file actions
36 lines (28 loc) · 822 Bytes
/
ValidParentheses.cs
File metadata and controls
36 lines (28 loc) · 822 Bytes
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
// linkt to kata: https://www.codewars.com/kata/52774a314c2333f0a7000688
using System;
using System.Collections.Generic;
public class Parentheses
{
public static bool ValidParentheses(string input)
{
if(input.Length < 0 || input.Length > 100) return false;
Stack<char> stack = new Stack<char>();
foreach(char c in input)
{
if(c == '(') { stack.Push(c); continue; }
if(c == ')') {
try
{
char stackPop = stack.Pop();
if(stackPop != '(') return false;
}
catch(InvalidOperationException)
{
return false;
}
}
}
if(stack.Count > 0) return false;
return true;
}
}