diff --git a/correct_parenthesis.java b/correct_parenthesis.java new file mode 100644 index 0000000..26e9502 --- /dev/null +++ b/correct_parenthesis.java @@ -0,0 +1,18 @@ +import java.util.*; +class Solution { + boolean solution(String s) { + boolean answer = true; + Deque parenthesis = new ArrayDeque<>(); + Map pair = Map.of(')','('); + for(char c : s.toCharArray()){ + if(c=='('){ + parenthesis.push(c); + }else{ + if(parenthesis.isEmpty() || pair.get(c)!=parenthesis.pop()){ + return false; + } + } + } + return parenthesis.isEmpty(); + } +}