Hard
Given a string containing just the characters '('
and ')'
, find the length of the longest valid (well-formed) parentheses substring.
Example 1:
Input: s = “(()”
Output: 2
Explanation: The longest valid parentheses substring is “()”.
Example 2:
Input: s = “)()())”
Output: 4
Explanation: The longest valid parentheses substring is “()()”.
Example 3:
Input: s = “”
Output: 0
Constraints:
0 <= s.length <= 3 * 104
s[i]
is '('
, or ')'
.To solve the “Longest Valid Parentheses” problem in Swift with a Solution
class, we can follow these steps:
Solution
class.longestValidParentheses
that takes a string s
as input and returns an integer representing the length of the longest valid parentheses substring.maxLen
to store the maximum length of valid parentheses found so far.-1
onto the stack to mark the starting point of a potential valid substring.'('
, push its index onto the stack.')'
:
maxLen
with the maximum of the current maxLen
and i - stack.peek()
, where i
is the current index and stack.peek()
is the index at the top of the stack.maxLen
.Here’s the implementation:
class Solution {
public func longestValidParentheses(_ s: String) -> Int {
var maxLen = 0
var left = 0
var right = 0
let n = s.count
let characters = Array(s)
for i in 0..<n {
if characters[i] == "(" {
left += 1
} else {
right += 1
}
if right > left {
left = 0
right = 0
}
if left == right {
maxLen = max(maxLen, left + right)
}
}
left = 0
right = 0
for i in (0..<n).reversed() {
if characters[i] == "(" {
left += 1
} else {
right += 1
}
if left > right {
left = 0
right = 0
}
if left == right {
maxLen = max(maxLen, left + right)
}
}
return maxLen
}
}
This implementation provides a solution to the “Longest Valid Parentheses” problem in Swift. It finds the length of the longest valid parentheses substring in the given string s
.