Skip to content

Instantly share code, notes, and snippets.

@d3ep4k
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d3ep4k/fd50d60e1b3d540ac210 to your computer and use it in GitHub Desktop.
Save d3ep4k/fd50d60e1b3d540ac210 to your computer and use it in GitHub Desktop.
Parenthesis Matching using stack
str = '{{{[({})[()]]}}}'
Stack s = new Stack()
i = 0
while(i< str.length()){
c = str[i]
if(c == '[' || c == '{' || c== '(')
s.push(c)
else if(!s.empty() &&
(c == '}' && s.peek() == '{'
|| c == ']' && s.peek() == '['
|| c == ')' && s.peek() == '(')){
s.pop()
}else{
break
}
i++
}
if(i == str.length() && s.empty()){
println 'matched parenthesis'
}else{
println 'failed to match parenthesis'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment