-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathValidParentheses.cs
47 lines (44 loc) · 2.62 KB
/
ValidParentheses.cs
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
// Source : https://leetcode.com/problems/valid-parentheses/
// Author : codeyu
// Date : 2016年10月9日 14:37:53
/***************************************************************************************
*
* Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
*
* The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
*
*
**********************************************************************************/
using System;
using System.Collections.Generic;
namespace Algorithms
{
public class Solution020
{
public static bool IsValid(string s)
{
if(s.Length < 2 || s.Length % 2 != 0)
{
return false;
}
var left = "([{";
var right = ")]}";
var st= new Stack<char>();
foreach(var c in s)
{
if(left.IndexOf(c) != -1)
{
st.Push(c);
}
else
{
if(st.Count == 0 || st.Pop() != left[right.IndexOf(c)])
{
return false;
}
}
}
return st.Count == 0;
}
}
}