I am trying to create a program that takes a string as an argument into its constructor. I need a method that checks whether the string is a balanced parenthesized expressio
///check Parenthesis
public boolean isValid(String s) {
Map map = new HashMap<>();
map.put('(', ')');
map.put('[', ']');
map.put('{', '}');
Stack stack = new Stack<>();
for(char c : s.toCharArray()){
if(map.containsKey(c)){
stack.push(c);
} else if(!stack.empty() && map.get(stack.peek())==c){
stack.pop();
} else {
return false;
}
}
return stack.empty();
}