Solved in c++ with 7 lines -DP


  • 0
    Q
    int dp[1000005], i, max = 0;
    memset(dp, 0, sizeof(dp));
    for(i = 1; i < s.size(); i++) {
        if(s[i] != ')' or s[i-dp[i]-1] != '(') continue;
        dp[i+1] = 2 + dp[i] + dp[i-dp[i]-1];
        if(dp[i+1] > max) max = dp[i+1];}
    return max;

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.