simple solution with explanation
Intuition\n Describe your first thoughts on how to solve this problem. \nwe have to return the longest length possible by the valid paranthesis.\n\n\n# Approach
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nwe have to return the longest length possible by the valid paranthesis.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nMy approach for this problem is actually very simple and easy to learn.\nFirst of all we will push into the stack the number -1.\nnow we will start the loop to get the paranthesis.\n```\nfor (int i = 0; i < s.length(); i++) {\n            if (s.charAt(i) == \'(\') {\n                stack.push(i);\n```\n\nIf we will get the open bracket then we will push into the stack.\nthen coming to the next:-\n\n```\n} else {\n                stack.pop();\n                if (stack.isEmpty()) {\n                    stack.push(i);\n```\nIf the stack becomes empty after popping, it means there was no unmatched \'(\' left. In this case, the current index i is pushed onto the stack as a new base for upcoming valid substrings.\n\n```\nelse {\n        max_len = Math.max(max_len, i - stack.peek());\n\n```\nIf the stack is not empty, it means there is an unmatched \'(\' available at the index of the top element. The difference i - stack.peek() calculates the length of the current valid substring, and max_len is updated to the maximum of its current value and this new length.\n\nNow lets have a example for a better understanding:-\n\nFor input: "(()())", the stack manipulation would look like this:\n\nInitial: stack = [-1]\nIndex 0: Push index of \'(\', so stack = [-1, 0]\nIndex 1: Push index of \'(\', so stack = [-1, 0, 1]\nIndex 2: Pop (matching \')\' with \'(\'), so stack = [-1, 0] and max_len = 2\nIndex 3: Push index of \'(\', so stack = [-1, 0, 3]\nIndex 4: Pop (matching \')\' with \'(\'), so stack = [-1, 0] and max_len = 4\nIndex 5: Pop (matching \')\' with \'(\'), so stack = [-1] and max_len = 6\nAfter the loop, max_len is 6, which is the correct length of the longest valid substring.\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int longestValidParentheses(String s) {\n        Stack<Integer> stack = new Stack<>();\n        stack.push(-1);\n        int max_len = 0;\n\n        for (int i = 0; i < s.length(); i++) {\n            if (s.charAt(i) == \'(\') {\n                stack.push(i);\n            } else {\n                stack.pop();\n                if (stack.isEmpty()) {\n                    stack.push(i);\n                } else {\n                    max_len = Math.max(max_len, i - stack.peek());\n                }\n            }\n        }\n\n        return max_len;        \n    }\n}\n```\n```python3 []\nclass Solution:\n    def longestValidParentheses(self, s: str) -> int:\n        stack = [-1]\n        max_len = 0\n\n        for i in range(len(s)):\n            if s[i] == "(":\n                stack.append(i)\n            else:\n                stack.pop()\n                if len(stack) == 0:\n                    stack.append(i)\n                else:\n                    max_len = max(max_len, i - stack[-1])\n        \n        return max_len\n```\n```cpp []\nclass Solution {\npublic:\n    int longestValidParentheses(string s) {\n        stack<int> st;\n        st.push(-1);\n        int max_len = 0;\n\n        for (int i = 0; i < s.length(); i++) {\n            if (s[i] == \'(\') {\n                st.push(i);\n            } else {\n                st.pop();\n                if (st.empty()) {\n                    st.push(i);\n                } else {\n                    max_len = max(max_len, i - st.top());\n                }\n            }\n        }\n\n        return max_len;        \n    }\n};\n```\n![d971a77c-9c71-4405-86b6-2727fd435122_1720044713.1503446.jpeg](https://assets.leetcode.com/users/images/cc450268-6f2f-48cc-b74d-545fac633ff1_1728565089.9930995.jpeg)\n\n
['String', 'Stack', 'C++', 'Java', 'Python3']