Last active
August 29, 2015 14:12
-
-
Save d3ep4k/fd50d60e1b3d540ac210 to your computer and use it in GitHub Desktop.
Parenthesis Matching using stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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