My C# solution using regular expression


  • 0
    M
    using System;
    using System.Text.RegularExpressions;
    
    public class Solution {
        public int LongestValidParentheses(string s) {
            Regex rgx = new Regex(@"\((-*)\)");
            string result = rgx.Replace(s, @"-$1-");
            while(!Object.ReferenceEquals(s, result)){
                s = result;
                result = rgx.Replace(s, @"-$1-");
            }
            int max = 0;
            foreach (Match match in Regex.Matches(s, @"-*"))
                max = Math.Max(match.Value.Length, max);
            return max;
        }
    }

Log in to reply
 

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