✅ [Accepted] Solution for Swift
swift\nclass Solution {\n    func longestValidParentheses(_ s: String) -> Int {\n        guard !s.isEmpty else { return 0 }\n        var val = 0, stack = [-1]\n
```swift\nclass Solution {\n    func longestValidParentheses(_ s: String) -> Int {\n        guard !s.isEmpty else { return 0 }\n        var val = 0, stack = [-1]\n        for (i, ch) in s.enumerated() {\n            guard ch != "(" else { stack.append(i); continue }\n            guard stack.count > 1 else { stack[0] = i; continue }\n            stack.removeLast()\n            val = max(val, i - stack.last!)\n        }\n        return val\n    }\n}\n```\n\n<hr>\n\n<p><details>\n<summary><img src="https://git.io/JDblm" height="24"> <b>TEST CASES</b></summary>\n\n<p><pre>\nResult: Executed 3 tests, with 0 failures (0 unexpected) in 0.010 (0.012) seconds\n</pre></p>\n\n```swift\nimport XCTest\n\nclass Tests: XCTestCase {\n    \n    private let solution = Solution()\n    \n    // The longest valid parentheses substring is "()".\n    func test0() {\n        let res = solution.longestValidParentheses("(()")\n        XCTAssertEqual(res, 2)\n    }\n    \n    // The longest valid parentheses substring is "()()".\n    func test1() {\n        let res = solution.longestValidParentheses(")()())")\n        XCTAssertEqual(res, 4)\n    }\n    \n    func test2() {\n        let res = solution.longestValidParentheses("")\n        XCTAssertEqual(res, 0)\n    }\n}\n\nTests.defaultTestSuite.run()\n```\n\n</details></p>
['Swift']