Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

Leetcode Longest Valid Parentheses problem solution

YASH PAL, 31 July 2024

In this Leetcode Longest Valid Parentheses problem solution we have Given a string containing just the characters ‘(‘ and ‘)’, find the length of the longest valid (well-formed) parentheses substring.

Leetcode Longest Valid Parentheses problem solution

Problem solution in Python.

 class Solution:
    def longestValidParentheses(self, s: str) -> int:
        if len(s) <= 1:
            return 0
        stack = []
        maxLen = 0
        stack.append(-1)
        for i in range(len(s)):
            if s[i] == '(':
                stack.append(i)
            elif s[i] == ')':
                if stack[-1] != -1 and s[stack[-1]] == '(':
                    stack.pop()
                    maxLen = max(i - stack[-1],maxLen)
                else:
                    stack.append(i)
                    
        return maxLen

Problem solution in Java.

class Solution {
    public int longestValidParentheses(String s) {
        int ans = 0;
        char l = '(';
        char r = ')';
        int i =0;
        int j;
        int[] save = new int[s.length()];
        
        while (i<s.length()){
            j = i;
            Stack<Integer> st = new Stack<Integer>();
            while(j<s.length()){
                if (s.charAt(j) == l) st.add(j);
                else{
                    if (st.isEmpty()) {
                        i = j+1;
                        break;
                    }
                    save[st.pop()] = 1;
                    save[j] = 1;
                    if (st.isEmpty()) {
                        i = j+1;
                        break;
                    }
                }
                if (j==s.length()-1) i=j+1;
                j++;
            }
        }
        
        i = 0;
        while (i<s.length()){
            if (save[i] != 1) {
                i++;
                continue;
            }
            j = i;
            while (j<s.length() && save[j] == save[i]) {
                j++;
            }
            if ((j-i)>ans) ans = (j-i);
            i = j;
        }
        
        return ans;
}
}

Problem solution in C++.

int longestValidParentheses(string s) {
        stack<int> list;
        list.push(-1);
        int maxa = 0;
        for(int i=0; i< s.length(); ++i){
            if(s[i] == '('){
                list.push(i);
            }
            else{
                list.pop();
                if(list.empty())
                    list.push(i);
                maxa = max(maxa, i - list.top());
                
            }
        }
        return maxa;
        
    }

Problem solution in C.

#define MAXN (32767)

int stck[MAXN],dp[MAXN];

int longestValidParentheses(char* s) {
    int i, ans = 0, top = 0;

    for(i = 0; s[i]; i++)
    {
        dp[i] = 0;
        if(s[i] == '(')
            stck[top++] = i;
        else if(top)
        {
            int tmp = stck[--top];
            dp[i] = i - tmp + 1 + (tmp?dp[tmp-1]:0);
            ans = ans < dp[i]?dp[i]:ans;
        }
    }
    return ans;
}

coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes