Advertisement
1988coder

301. Remove Invalid Parentheses

Jan 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1.  
  2. // LeetCode URL: https://leetcode.com/problems/remove-invalid-parentheses/
  3. import java.util.ArrayList;
  4. import java.util.HashSet;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Queue;
  8.  
  9. /**
  10.  * BFS
  11.  *
  12.  * Refer:
  13.  * https://leetcode.com/problems/remove-invalid-parentheses/discuss/75032/Share-my-Java-BFS-solution
  14.  *
  15.  * Time Complexity: O(n * 2^n).
  16.  *
  17.  * In BFS we handle the states level by level, in the worst case, we need to
  18.  * handle all the levels, we can analyze the time complexity level by level and
  19.  * add them up to get the final complexity.
  20.  *
  21.  * On the first level, there's only one string which is the input string s,
  22.  * let's say the length of it is n, to check whether it's valid, we need O(n)
  23.  * time. On the second level, we remove one ( or ) from the first level, so
  24.  * there are C(n, n-1) new strings, each of them has n-1 characters, and for
  25.  * each string, we need to check whether it's valid or not, thus the total time
  26.  * complexity on this level is (n-1) x C(n, n-1). Come to the third level, total
  27.  * time complexity is (n-2) x C(n, n-2), so on and so forth...
  28.  *
  29.  * Finally we have this formula:
  30.  *
  31.  * T(n) = n x C(n, n) + (n-1) x C(n, n-1) + ... + 1 x C(n, 1) = n x 2^(n-1).
  32.  *
  33.  *
  34.  * Space Complexity: O(n/2 * (n C (n/2))). Upper bound = O(n * (2*e)^(n/2))
  35.  *
  36.  * At n/2 level, the space required will be maximum. Refer below link for proof
  37.  * and calculation of nCk
  38.  * https://www.johndcook.com/blog/2008/11/10/bounds-on-binomial-coefficients/
  39.  */
  40. class Solution {
  41.     public List<String> removeInvalidParentheses(String s) {
  42.         ArrayList<String> result = new ArrayList<>();
  43.         if (s == null) {
  44.             return result;
  45.         }
  46.  
  47.         Queue<String> queue = new LinkedList<>();
  48.         HashSet<String> visited = new HashSet<>();
  49.         boolean foundValid = false;
  50.         queue.offer(s);
  51.         visited.add(s);
  52.  
  53.         while (!queue.isEmpty()) {
  54.             int queueSize = queue.size();
  55.             for (int i = 0; i < queueSize; i++) {
  56.                 String cur = queue.poll();
  57.  
  58.                 if (isValid(cur)) {
  59.                     result.add(cur);
  60.                     foundValid = true;
  61.                 }
  62.                 if (foundValid) {
  63.                     continue;
  64.                 }
  65.  
  66.                 for (int j = 0; j < cur.length(); j++) {
  67.                     if (cur.charAt(j) != '(' && cur.charAt(j) != ')') {
  68.                         continue;
  69.                     }
  70.                     String newCur = cur.substring(0, j) + cur.substring(j + 1);
  71.                     if (!visited.contains(newCur)) {
  72.                         queue.offer(newCur);
  73.                         visited.add(newCur);
  74.                     }
  75.                 }
  76.             }
  77.  
  78.             if (foundValid) {
  79.                 break;
  80.             }
  81.         }
  82.  
  83.         return result;
  84.     }
  85.  
  86.     private boolean isValid(String s) {
  87.         int count = 0;
  88.         for (char c : s.toCharArray()) {
  89.             if (c == '(') {
  90.                 count++;
  91.             } else if (c == ')') {
  92.                 count--;
  93.                 if (count < 0) {
  94.                     return false;
  95.                 }
  96.             }
  97.         }
  98.         return count == 0;
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement