Skip to content

Instantly share code, notes, and snippets.

@gdyrrahitis

gdyrrahitis/Analyzer.cs Secret

Last active Nov 9, 2018
Embed
What would you like to do?
public static class Analyzer
{
public static bool HasValidOpeningClosingBrackets(string input)
{
var stack = new Stack<char>(input.Length);
// Test each character in string input
foreach (var character in input)
{
switch (character)
{
// When opening bracket, push it in stack
case '{':
case '[':
case '(':
stack.Push(character);
break;
// When closing bracket, pop it and test it
case '}':
case ']':
case ')':
var stored = stack.Pop();
// If the popped item does not match, check has failed
if (stored == '{' && character != '}' ||
stored == '[' && character != ']' ||
stored == '(' && character != ')')
{
return false;
}
break;
default:
break;
}
}
// Check hasn't failed yet, input is okay
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.