<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vishal Kumar</title>
    <description>The latest articles on DEV Community by Vishal Kumar (@imvishalksingh).</description>
    <link>https://dev.to/imvishalksingh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1406571%2F39acc747-05ac-4834-82ec-978cdf1be8b6.jpeg</url>
      <title>DEV Community: Vishal Kumar</title>
      <link>https://dev.to/imvishalksingh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/imvishalksingh"/>
    <language>en</language>
    <item>
      <title>Longest Valid Parentheses problem solution</title>
      <dc:creator>Vishal Kumar</dc:creator>
      <pubDate>Thu, 04 Apr 2024 13:45:47 +0000</pubDate>
      <link>https://dev.to/imvishalksingh/longest-valid-parentheses-problem-solution-45g4</link>
      <guid>https://dev.to/imvishalksingh/longest-valid-parentheses-problem-solution-45g4</guid>
      <description>&lt;p&gt;&lt;strong&gt;Given a string containing just the characters '(' and ')', return the length of the longest valid (well-formed) parentheses substring.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: s = "(()"&lt;br&gt;
Output: 2&lt;br&gt;
Explanation: The longest valid parentheses substring is "()".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: s = ")()())"&lt;br&gt;
Output: 4&lt;br&gt;
Explanation: The longest valid parentheses substring is "()()".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: s = ""&lt;br&gt;
Output: 0&lt;/p&gt;

&lt;p&gt;Constraints:&lt;/p&gt;

&lt;p&gt;0 &amp;lt;= s.length &amp;lt;= 3 * 104&lt;br&gt;
s[i] is '(', or ')'.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution :&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Approach&lt;br&gt;
Here the approach is nothing but we are using a stack and when we encounter an opening brace then we push the index of it into the stack and whenever we touch a closing brace then we see the top of the stack if it's size is one then it means the closing braces have dominated the opening brace. We then edit the top value of the stack to the index of the closing brace.&lt;br&gt;
This method is clearly depicted in the picture as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzlph0eoerez6tfdnmkuh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzlph0eoerez6tfdnmkuh.png" alt="Image description" width="743" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
here answer is given as the line ans = max(ans , index - stk.top()) only when the size of stack is not 1 and there is a closing brace encountered.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    int longestValidParentheses(string s) {
        stack&amp;lt;int&amp;gt;stk;
        stk.push(-1);
        int ans = 0;
        for(int i = 0 ; i &amp;lt; s.size();  i++)
        {
            if(s[i] == '(')
                stk.push(i);
            else
            {
                if(stk.size() == 1)
                    stk.top() = i;
                else
                {
                    stk.pop();
                    ans = max(ans , i - stk.top());
                }
            }
        }
        return ans;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int longestValidParentheses(String s) {
    int leftCount = 0;
    int rightCount = 0;
    int maxLength = 0;

    for (int i = 0; i &amp;lt; s.length(); i++) {
        if (s.charAt(i) == '(') {
            leftCount++;
        } else {
            rightCount++;
        }

        if (leftCount == rightCount) {
            maxLength = Math.max(maxLength, 2 * rightCount);
        } else if (rightCount &amp;gt; leftCount) {
            leftCount = rightCount = 0;
        }
    }

    leftCount = rightCount = 0;

    for (int i = s.length() - 1; i &amp;gt;= 0; i--) {
        if (s.charAt(i) == '(') {
            leftCount++;
        } else {
            rightCount++;
        }

        if (leftCount == rightCount) {
            maxLength = Math.max(maxLength, 2 * leftCount);
        } else if (leftCount &amp;gt; rightCount) {
            leftCount = rightCount = 0;
        }
    }

    return maxLength;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def longestValidParentheses(self, s: str) -&amp;gt; int:
        stack=[]
        l=['0']*len(s)
        for ind,i in enumerate(s):
            if i=='(':
                stack.append(ind)
            else:
                if stack:
                    l[stack.pop()]='1'
                    l[ind]='1'
        return max(len(i) for i in ''.join(l).split('0'))

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Complexity&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Time complexity:Here the complexity would be $$O(n)$$ as we are using only a single loop with a stack only so this runs in a linear complexity.&lt;/li&gt;
&lt;li&gt;Space complexity:Here the space complexity would be $O(n)$ as we are using just a stack that too store the elements in the worst case it goes to that complexity.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>leetcode</category>
      <category>beginners</category>
      <category>java</category>
      <category>cpp</category>
    </item>
  </channel>
</rss>
