public int numPermsDISequence(String S) { int n=S.length(); int [][]dp=new int [n+1][n+1]; dp[0][0]=1; int M=(int) (Math.pow(10, 9)+7); //dp[i][j]i表示取到s的下标,j表示结尾取的数值 for(int i=0;i<n;i++){ if(S.charAt(i)=='I'){ for(int j=1;j<=i+1;j++) dp[i+1][j]=(dp[i+1][j-1]+dp[i][j-1])%M; }else{ for(int j=i;j>=0;j--) dp[i+1][j]=(dp[i+1][j+1]+dp[i][j])%M; } } int res=0; for(int i=0;i<=n;i++) res=(res+dp[n][i])%M; return res; }
public int longestValidParentheses(String s) { if (s.length() < 2) return 0; int[] dp = new int[s.length()]; int res = 0; for (int i = 1; i < s.length(); i++) { if (s.charAt(i) == ')') { if (s.charAt(i-1) == '(') dp[i] = 2 + (i - 2 >= 0 ? dp[i-2] : 0); else if (i - 1 - dp[i-1] >= 0 && s.charAt(i - 1 - dp[i-1]) == '(') dp[i] = 2 + dp[i-1] + (i - 2 - dp[i-1] >= 0 ? dp[i-2-dp[i-1]] : 0); res = Math.max(res, dp[i]); } } return res; }