2.3 All permutation of parentheses with priority restriction

Subject to the priority restriction: {} higher than <> higher than ().

Solution: when adding a new left, first check the priority of the current parenthesis against stack.peek(), if matches, then push to the stack, and add it to sb.

for (int i = 0; i < remain.length; i++) {
     if (i % 2 == 0) {   // check condition before adding a new left
       if (remain[i] > 0 && (stack.isEmpty() || stack.peekFirst() > PS[i]) {
       // same as in 2.2
       }
     }
}

Last updated

Was this helpful?