Advertisement
1988coder

Longest Valid Parentheses

Sep 30th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. /*
  2. Sliding Window (or by counting the open and close brackets)
  3. Time Complexity : O(n) -> whole string will be traversed twice.
  4. Space Complexity : O(1) -> Constant space will be used.
  5. */
  6. class Solution {
  7.     public int longestValidParentheses(String s) {
  8.         if (s == null || s.length() <= 1) {
  9.             return 0;
  10.         }
  11.        
  12.         int open = 0;
  13.         int close = 0;
  14.         int maxLength = 0;
  15.        
  16.         for (int i = 0; i < s.length(); i++) {
  17.             if (s.charAt(i) == '(') {
  18.                 open++;
  19.             } else {
  20.                 close++;
  21.             }
  22.             if (open == close) {
  23.                 maxLength = Math.max(maxLength, 2 * open);
  24.             } else if (close > open) {
  25.                 open = 0;
  26.                 close = 0;
  27.             }
  28.         }
  29.        
  30.         open = 0;
  31.         close = 0;
  32.        
  33.         for (int i = s.length() - 1; i >= 0; i--) {
  34.             if (s.charAt(i) == '(') {
  35.                 open++;
  36.             } else {
  37.                 close++;
  38.             }
  39.             if (open == close) {
  40.                 maxLength = Math.max(maxLength, 2 * open);
  41.             } else if (open > close) {
  42.                 open = 0;
  43.                 close = 0;
  44.             }
  45.         }
  46.        
  47.         return maxLength;
  48.     }
  49. }
  50.  
  51. /*
  52. Using stack.
  53. Time Complexity : O(n) -> whole string will be traversed once.
  54. Space Complexity : O(n) -> In worst case, size of the stack can grow upto length of the string (n).
  55. */
  56. // class Solution {
  57. //     public int longestValidParentheses(String s) {
  58. //         if (s == null || s.length() <= 1) {
  59. //             return 0;
  60. //         }
  61.        
  62. //         Stack<Integer> stack = new Stack<>();
  63. //         stack.push(-1);
  64.  
  65. //         int maxLength = 0;
  66.        
  67. //         for (int i = 0; i < s.length(); i++) {
  68. //             if (s.charAt(i) == '(') {
  69. //                 stack.push(i);
  70. //             } else {
  71. //                 stack.pop();
  72. //                 if (stack.empty()) {
  73. //                     stack.push(i);
  74. //                 } else {
  75. //                     maxLength = Math.max(maxLength, i - stack.peek());
  76. //                 }
  77. //             }
  78. //         }
  79.        
  80. //         return maxLength;
  81. //     }
  82. // }
  83.  
  84. /*
  85. Dynamic Programming
  86. Time Complexity : O(n) -> whole string will be traversed once.
  87. Space Complexity : O(n) -> Size of the DP array.
  88. */
  89. // class Solution {
  90. //     public int longestValidParentheses(String s) {
  91. //         if (s == null || s.length() <= 1) {
  92. //             return 0;
  93. //         }
  94.        
  95. //         int[] dp = new int[s.length() + 1];
  96. //         int maxLength = 0;
  97.        
  98. //         for (int i = 1; i < s.length(); i++) {
  99. //             int dpIndex = i+1;
  100. //             if (s.charAt(i) == ')') {
  101. //                 if (s.charAt(i-1) == '(') {
  102. //                     dp[dpIndex] = dp[dpIndex-2] + 2;
  103. //                 } else if (i - dp[dpIndex-1] > 0 && s.charAt(i - dp[dpIndex-1] - 1) == '(') {
  104. //                     dp[dpIndex] = dp[dpIndex - 1] + 2 + dp[dpIndex - dp[dpIndex-1] - 2];
  105. //                 }
  106. //                 maxLength = Math.max(maxLength, dp[dpIndex]);
  107. //             }
  108. //         }
  109.        
  110. //         return maxLength;
  111. //     }
  112. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement