|
| 1 | +package org.sean.array; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Stack; |
| 5 | + |
| 6 | +/*** |
| 7 | + * 678. Valid Parenthesis String |
| 8 | + */ |
| 9 | +public class ParenthesisValidator { |
| 10 | + // region Counting O(N) |
| 11 | + public boolean checkValidString(String s) { |
| 12 | + int minOp = 0; |
| 13 | + int maxOp = 0; |
| 14 | + |
| 15 | + for (char c : s.toCharArray()) { |
| 16 | + if (c == '(') ++minOp; |
| 17 | + else --minOp; |
| 18 | + |
| 19 | + if (c != ')') ++maxOp; |
| 20 | + else --maxOp; |
| 21 | + |
| 22 | + if (maxOp < 0) |
| 23 | + return false; |
| 24 | + |
| 25 | + minOp = Math.max(0, minOp); |
| 26 | + } |
| 27 | + return minOp == 0; |
| 28 | + } |
| 29 | + // endregion |
| 30 | + |
| 31 | + // region DP (Top-Down) O(N^3) |
| 32 | + public boolean checkValidString1(String s) { |
| 33 | + // L[ XXX ]R |
| 34 | + // [LxR LxR] |
| 35 | + int len = s.length(); |
| 36 | + caches = new int[len][len]; |
| 37 | + for (int[] cacheResult : caches) { |
| 38 | + Arrays.fill(cacheResult, -1); |
| 39 | + } |
| 40 | + |
| 41 | + return checkValidStringHelper(s, 0, len - 1); |
| 42 | + } |
| 43 | + |
| 44 | + private int[][] caches; |
| 45 | + |
| 46 | + private boolean checkValidStringHelper(String s, int left, int right) { |
| 47 | + if (left > right) {// empty string ?! |
| 48 | + caches[left][right] = 1; |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + if (caches[left][right] >= 0) |
| 53 | + return caches[left][right] == 1; |
| 54 | + |
| 55 | + char leftCh = s.charAt(left); |
| 56 | + char rightCh = s.charAt(right); |
| 57 | + if (left == right) { |
| 58 | + caches[left][right] = leftCh == '*' ? 1 : 0; |
| 59 | + return caches[left][right] == 1; |
| 60 | + } |
| 61 | + |
| 62 | + // // x[xxx]x |
| 63 | + if ((leftCh == '(' || leftCh == '*') |
| 64 | + && (rightCh == ')' || rightCh == '*') |
| 65 | + && checkValidStringHelper(s, left + 1, right - 1)) { |
| 66 | + |
| 67 | + caches[left][right] = 1; |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + for (int i = left; i < right; i++) { |
| 72 | + if (checkValidStringHelper(s, left, i) && checkValidStringHelper(s, i + 1, right)) { |
| 73 | + caches[left][right] = 1; |
| 74 | + return true; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + caches[left][right] = 0; |
| 79 | + return false; |
| 80 | + } |
| 81 | + // endregion |
| 82 | + |
| 83 | + // region Double Stacks O(N) |
| 84 | + public boolean checkValidString0(String s) { |
| 85 | + Stack<Integer> stackLeft = new Stack<>(); |
| 86 | + Stack<Integer> stackStar = new Stack<>(); |
| 87 | + for (int i = 0; i < s.length(); i++) { |
| 88 | + char c = s.charAt(i); |
| 89 | + if (c == '(') { |
| 90 | + stackLeft.push(i); |
| 91 | + } else if (c == '*') { |
| 92 | + stackStar.push(i); |
| 93 | + } else { |
| 94 | + // balance ')' |
| 95 | + if (!stackLeft.empty()) { |
| 96 | + stackLeft.pop(); |
| 97 | + } else if (!stackStar.empty()) { |
| 98 | + stackStar.pop(); |
| 99 | + } else { |
| 100 | + return false; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + while (!stackLeft.empty() && !stackStar.empty()) { |
| 106 | + if (stackLeft.pop() > stackStar.pop()) { // any '*' before '(' ? |
| 107 | + return false; |
| 108 | + } |
| 109 | + } |
| 110 | + return stackLeft.empty(); |
| 111 | + } |
| 112 | + // endregion |
| 113 | +} |
0 commit comments