LONGEST VALID PARENTHESES

Problem:
Given a string containing just the characters  '(' and  ')', find the length of the longest valid (well-formed) parentheses substring.
For  "(()", the longest valid parentheses substring is  "()", which has length = 2.
Another example is  ")()())", where the longest valid parentheses substring is  "()()", which has length = 4.
Analysis:
Method 1: Stack
We can use stack to solve this problem. We scan the string from left to right. If the current character is (, we push the index of it into stack. If the current character is ), if the ) cannot match a ( when stack is empty, we record the position of this ); if the ) can match a ( in the top of stack, we pop the (, if there’s no more index in stack, the current length is i – last. if there’s at least one index in stack, the current length is i –  stack.peek(). Whenever the current length is computed, compare it with the max length.
Method 2: Dynamic Programming
Use an 1d array to use dynamic programming. dp[i] is the length of longestvalidparenthese starting at i.

We can fill the dp array from the right to left. dp[s.length()-1] is definitely 0. Consider the case of “((….))“, for position i, the next position after the substring represented by dp[i+1] is j = i + dp[i+1] + 1. If dp[j] is a matched “)”, then dp[i] is dp[i+1] + 2. There could also be the case that since the “)” at dp[j] is matched, the substring after dp[j] is also connected, Consider ((….))(….), in this case, we add dp[j+1] to dp[i].
The total time complexity for dynamic programming is O(n), and space complexity is O(1).

Update 1/16/2015:

Method 2 is the solution I’m most comfortable with.

 

Solution:
Method 1: Stack:
1:  public class Solution {  
2:    public int longestValidParentheses(String s) {  
3:      if(s == null || s.length() == 0){  
4:        return 0;  
5:      }  
6:      int maxLen = 0;  
7:      int last = -1;  
8:      Stack<Integer> stack = new Stack<Integer>();  
9:      for(int i = 0; i < s.length(); i++){  
10:        char c = s.charAt(i);  
11:        if(c == '('){  
12:          stack.add(i);  
13:        }  
14:        else {  
15:          //the ( is unmatched, it means the end of the previous group  
16:          if(stack.isEmpty()){  
17:            last = i;  
18:          }  
19:          else{  
20:            stack.pop();  
21:            if(stack.isEmpty()){  
22:              maxLen = Math.max(maxLen, i - last);  
23:            } else{  
24:              maxLen = Math.max(maxLen, i - stack.peek());  
25:            }  
26:          }  
27:        }  
28:      }  
29:      return maxLen;    
30:    }  
31:  }  
Method 2: Dynamic Programming:

1:  public class Solution {  
2:    public int longestValidParentheses(String s) {  
3:      if(s == null || s.length() == 0){  
4:        return 0;  
5:      }  
6:      int maxLen = 0;  
7:      //start a dp array, dp[i] is the length of the valid longestvalidparenthese starting at i  
8:      int[] dp = new int[s.length()];  
9:      //any string starting at the end is not valid  
10:      dp[dp.length-1] = 0;  
11:      for(int i = dp.length -2; i >= 0; i--){  
12:        if(s.charAt(i) == ')'){  
13:          dp[i] = 0;  
14:          continue;  
15:        }  
16:        //the first character after the substring represented by dp[i+1]  
17:        int j = i + dp[i+1] + 1;  
18:        if(j < s.length() && s.charAt(j) == ')'){  
19:          // for the situation like ((....))  
20:          dp[i] = dp[i+1] + 2;  
21:          // for the situation like ((....))()  
22:          if(j + 1 < s.length()){  
23:            dp[i] += dp[j+1];  
24:          }  
25:        }  
26:        maxLen = Math.max(dp[i], maxLen);  
27:      }  
28:      return maxLen;    
29:    }  
30:  }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值