Skip to content

Instantly share code, notes, and snippets.

@d3ep4k
Last active August 29, 2015 14:12
Show Gist options
  • 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