|
| 1 | +--- |
| 2 | +id: longest-valid-parentheses |
| 3 | +title: longest-valid-parentheses |
| 4 | +sidebar_label: 0032 longest-valid-parentheses |
| 5 | +tags: |
| 6 | + - stack |
| 7 | + - LeetCode |
| 8 | + - Java |
| 9 | + - Python |
| 10 | + - C++ |
| 11 | +description: This is a solution to the longest-valid-parentheses on LeetCode |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem Description |
| 15 | + |
| 16 | +Given a string containing just the characters `'('` and `')'`, return the length of the longest valid (well-formed) parentheses |
| 17 | +`substring` |
| 18 | + |
| 19 | + |
| 20 | +### Examples |
| 21 | + |
| 22 | +**Example 1:** |
| 23 | + |
| 24 | +``` |
| 25 | +
|
| 26 | +Input: s = "(()" |
| 27 | +Output: 2 |
| 28 | +Explanation: The longest valid parentheses substring is "()". |
| 29 | +
|
| 30 | +``` |
| 31 | + |
| 32 | +**Example 2:** |
| 33 | + |
| 34 | + |
| 35 | +``` |
| 36 | +Input: s = ")()())" |
| 37 | +Output: 4 |
| 38 | +Explanation: The longest valid parentheses substring is "()()". |
| 39 | +``` |
| 40 | + |
| 41 | +**Example 3:** |
| 42 | + |
| 43 | + |
| 44 | +``` |
| 45 | +Input: s = "" |
| 46 | +Output: 0 |
| 47 | +``` |
| 48 | + |
| 49 | + |
| 50 | +### Constraints |
| 51 | + |
| 52 | +- $1 \leq \text{nums.length} \leq 105$ |
| 53 | + |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +## Solution for Longest Valid Parentheses |
| 58 | + |
| 59 | + |
| 60 | +### Intuition |
| 61 | + |
| 62 | +The intuition behind the given code is to use a stack to efficiently track the indices of opening parentheses. |
| 63 | + |
| 64 | + |
| 65 | +### Approach |
| 66 | + |
| 67 | + |
| 68 | + - It initializes a variable maxCount to 0 to store the length of the longest valid parentheses substring. |
| 69 | + |
| 70 | + - It initializes a stack (st) and pushes -1 onto the stack. The stack is used to keep track of the indices of opening parentheses. |
| 71 | + |
| 72 | + - It iterates through each character in the input string s. |
| 73 | + |
| 74 | + - If the current character is an opening parenthesis '(', it pushes its index onto the stack. |
| 75 | + |
| 76 | + - If the current character is a closing parenthesis ')', it pops the top element from the stack, representing the index of the matching opening parenthesis. |
| 77 | + |
| 78 | + - If the stack becomes empty after popping, it pushes the current index onto the stack (unmatched closing parenthesis). |
| 79 | + |
| 80 | + - If the stack is not empty, it calculates the length of the valid parentheses substring by subtracting the index of the matching opening parenthesis from the current index. It updates maxCount with the maximum length encountered so far. |
| 81 | + |
| 82 | + - After iterating through all characters, it returns maxCount, representing the length of the longest valid parentheses substring. |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +#### Code in Different Languages |
| 89 | + |
| 90 | +<Tabs> |
| 91 | + <TabItem value="Python" label="Python"> |
| 92 | + <SolutionAuthor name="@parikhitkurmi"/> |
| 93 | + |
| 94 | + ```python |
| 95 | +//python |
| 96 | + |
| 97 | +class Solution: |
| 98 | + def longestValidParentheses(self, s: str) -> int: |
| 99 | + max_length = 0 |
| 100 | + stck=[-1] # initialize with a start index |
| 101 | + for i in range(len(s)): |
| 102 | + if s[i] == '(': |
| 103 | + stck.append(i) |
| 104 | + else: |
| 105 | + stck.pop() |
| 106 | + if not stck: # if popped -1, add a new start index |
| 107 | + stck.append(i) |
| 108 | + else: |
| 109 | + max_length=max(max_length, i-stck[-1]) # update the length of the valid substring |
| 110 | + return max_length |
| 111 | +``` |
| 112 | + </TabItem> |
| 113 | + <TabItem value="Java" label="Java"> |
| 114 | + <SolutionAuthor name="@parikhitkurmi"/> |
| 115 | + |
| 116 | + ```java |
| 117 | +//java |
| 118 | + |
| 119 | + |
| 120 | +class Solution { |
| 121 | + public int longestValidParentheses(String s) { |
| 122 | + Stack<Integer> stack = new Stack<>(); |
| 123 | + stack.push(-1); |
| 124 | + int max=0; |
| 125 | + for(int i=0;i<s.length();i++){ |
| 126 | + Character ch = s.charAt(i); |
| 127 | + if(ch == '('){ |
| 128 | + stack.push(i); |
| 129 | + } |
| 130 | + else{ |
| 131 | + stack.pop(); |
| 132 | + if(stack.isEmpty()){ |
| 133 | + stack.push(i); |
| 134 | + } |
| 135 | + max = Math.max(max,i-stack.peek()); |
| 136 | + } |
| 137 | + } |
| 138 | + return max; |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +``` |
| 143 | +</TabItem> |
| 144 | +<TabItem value="C++" label="C++"> |
| 145 | +<SolutionAuthor name="@parikhitkurmi"/> |
| 146 | + |
| 147 | + ```cpp |
| 148 | +//cpp |
| 149 | +class Solution { |
| 150 | +public: |
| 151 | + int longestValidParentheses(string s) { |
| 152 | + int n = s.size(); |
| 153 | + stack <int>st; |
| 154 | + st.push(-1); |
| 155 | + int len_max = 0; |
| 156 | + for(int i=0;i<s.size();i++){ |
| 157 | + if(s[i]=='('){ |
| 158 | + st.push(i); |
| 159 | + } |
| 160 | + |
| 161 | + else{ |
| 162 | + st.pop(); |
| 163 | + if(st.empty()){ |
| 164 | + st.push(i); |
| 165 | + } |
| 166 | + else { |
| 167 | + int len = i-st.top(); |
| 168 | + len_max = max(len,len_max); |
| 169 | + |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + } |
| 174 | + return len_max; |
| 175 | + } |
| 176 | +}; |
| 177 | + |
| 178 | +``` |
| 179 | +
|
| 180 | + </TabItem> |
| 181 | +</Tabs> |
| 182 | +
|
| 183 | +
|
| 184 | +# Complexity |
| 185 | +
|
| 186 | + - Time complexity: |
| 187 | + `$O(n)$` time complexity as we iterate over the string |
| 188 | +
|
| 189 | + - Space complexity: |
| 190 | + `$O(n)$` space complexity because we used stack |
| 191 | +## References |
| 192 | +
|
| 193 | +- **LeetCode Problem:** [Continuous Subarray Sum](https://leetcode.com/problems/longest-valid-parentheses/) |
| 194 | +- **Solution Link:** [Continuous Subarray Sum](https://leetcode.com/problems/longest-valid-parentheses/submissions/) |
| 195 | +- **Authors GeeksforGeeks Profile:** [parikhit kurmi](https://www.geeksforgeeks.org/user/sololeveler673/) |
| 196 | +- **Authors Leetcode:** [parikhit kurmi](https://leetcode.com/u/parikhitkurmi14/) |
0 commit comments