🚀 ✅100% , Detailed explaination 🔥 with Pictures ✅ 🔥 🚀O(n) ,  in C++ , Java , Python , stack. 🔥
Approach\n- 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 wheneve
# Approach\n- 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.\n>- This method is clearly depicted in the picture as shown below.\n\n![pic1.png](https://assets.leetcode.com/users/images/28c7fe2b-0003-4027-9d8f-94b1abadf7c4_1681156598.7949007.png)\n\n>- 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.\n\n---\n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int longestValidParentheses(string s) {\n        stack<int>stk;\n        stk.push(-1);\n        int ans = 0;\n        for(int i = 0 ; i < s.size();  i++)\n        {\n            if(s[i] == \'(\')\n                stk.push(i);\n            else\n            {\n                if(stk.size() == 1)\n                    stk.top() = i;\n                else\n                {\n                    stk.pop();\n                    ans = max(ans , i - stk.top());\n                }\n            }\n        }\n        return ans;\n    }\n};\n```\n```java []\nclass Solution {\n    public int longestValidParentheses(String s) {\n    int leftCount = 0;\n    int rightCount = 0;\n    int maxLength = 0;\n    \n    for (int i = 0; i < s.length(); i++) {\n        if (s.charAt(i) == \'(\') {\n            leftCount++;\n        } else {\n            rightCount++;\n        }\n        \n        if (leftCount == rightCount) {\n            maxLength = Math.max(maxLength, 2 * rightCount);\n        } else if (rightCount > leftCount) {\n            leftCount = rightCount = 0;\n        }\n    }\n    \n    leftCount = rightCount = 0;\n    \n    for (int i = s.length() - 1; i >= 0; i--) {\n        if (s.charAt(i) == \'(\') {\n            leftCount++;\n        } else {\n            rightCount++;\n        }\n        \n        if (leftCount == rightCount) {\n            maxLength = Math.max(maxLength, 2 * leftCount);\n        } else if (leftCount > rightCount) {\n            leftCount = rightCount = 0;\n        }\n    }\n    \n    return maxLength;\n    }\n}\n```\n```python []\nclass Solution:\n    def longestValidParentheses(self, s: str) -> int:\n        stack=[]\n        l=[\'0\']*len(s)\n        for ind,i in enumerate(s):\n            if i==\'(\':\n                stack.append(ind)\n            else:\n                if stack:\n                    l[stack.pop()]=\'1\'\n                    l[ind]=\'1\'\n        return max(len(i) for i in \'\'.join(l).split(\'0\'))\n```\n\n---\n\n\n\n# Complexity\n>- 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.\n\n>- 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.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n---\n\n\n**IF YOU LIKE THE SOLUTION THEN PLEASE UPVOTE MY SOLUTION BECAUSE IT GIVES ME MOTIVATION TO REGULARLY POST THE SOLUTION.**\n\n![UPVOTE.jpg](https://assets.leetcode.com/users/images/d53e3e8f-f0ff-4dbb-bf1f-f44e71c7e735_1681156831.395462.jpeg)\n
['Dynamic Programming', 'Stack', 'Python', 'C++', 'Java']