Skip to content

Instantly share code, notes, and snippets.

@abhilash0001
Created January 14, 2020 18:04
Show Gist options
  • Save abhilash0001/cfca213ab1a83554781096dd607d836b to your computer and use it in GitHub Desktop.
Save abhilash0001/cfca213ab1a83554781096dd607d836b to your computer and use it in GitHub Desktop.
public class ParenthesisBalancing
{
public bool checkIfParanthesisIsBalanced(string str)
{
return !string.IsNullOrEmpty(str) ? check(str) : false;
}
private bool check(string str)
{
var s = new Stack();
for (int i = 0; i < str.Length; i++)
{
if (str[i] == '{' || str[i] == '(' || str[i] == '[')
{
s.push(str[i]);
}
else if (str[i] == '}' || str[i] == ')' || str[i] == ']')
{
if (s.count == 0 || s.peek() != str[i])
{
return false;
}
s.pop();
}
}
return s.count == 0 ? true : false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment