-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution014.cs
57 lines (47 loc) · 1.58 KB
/
solution014.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
47
48
49
50
51
52
53
54
55
56
57
// Valid Parentheses
// Difficulty: Easy
// Runtime: 68 ms, faster than 95.13% of C# online submissions for Valid Parentheses.
// Memory Usage: 37.4 MB, less than 25.31% of C# online submissions for Valid Parentheses.
public class Solution {
public bool IsValid(string s) {
var openingBrackets = new List<char>() { '(', '[', '{', '<' };
var closingBrackets = new List<char>() { ')', ']', '}', '>' };
var allMissingBrackets = new List<List<char>>();
var openedBrackets = new List<char>();
foreach (var bracket in s)
{
if (openingBrackets.Contains(bracket))
{
openedBrackets.Add(bracket);
}
else
{
var correspondingOpeningBracket = openingBrackets.ElementAt(closingBrackets.IndexOf(bracket));
int index = openedBrackets.FindLastIndex(x => x == correspondingOpeningBracket);
// Closing bracket and no matching opening bracket exists
if (index == -1)
{
return false;
}
if (index == openedBrackets.Count - 1)
{
Console.WriteLine(index);
openedBrackets.RemoveAt(index);
}
else
{
break;
}
}
}
// Evaluate if any opened brackets are legt
if (openedBrackets.Count == 0)
{
return true;
}
else
{
return false;
}
}
}