✔️ 100% Fastest Swift Solution
\nclass Solution {\n    func longestValidParentheses(_ s: String) -> Int {\n\t\tvar string = Array(s)\n\n\n\t\tfunc reverse(_ string: [Character]) -> [Character
```\nclass Solution {\n    func longestValidParentheses(_ s: String) -> Int {\n\t\tvar string = Array(s)\n\n\n\t\tfunc reverse(_ string: [Character]) -> [Character] {\n\t\t\tvar res: [Character] = []\n\t\t\tfor c in string.reversed() { res.append(c == "(" ? ")" : "(") }\n\t\t\treturn res\n\t\t}\n\t\t\n\n\t\tfunc _max(_ string: [Character]) -> Int {\n\t\t\tvar max = 0\n\t\t\tvar tmp = 0\n\t\t\tvar lastContinuousIndex = 0\n\t\t\tvar stack: [Character] = []\n\n\t\t\tfor (i, c) in string.enumerated() {\n\t\t\t\tif c == "(" {\n\t\t\t\t\tif stack.count == 0 { lastContinuousIndex = i }\n\t\t\t\t\tstack.append(c)\n\t\t\t\t} else {\n\t\t\t\t\tlet index = stack.count - 1\n\t\t\t\t\tif index >= 0 && stack[index] == "(" {\n\t\t\t\t\t\tstack.remove(at: index)\n\t\t\t\t\t\ttmp += 2\n\t\t\t\t\t\tmax = tmp > max ? tmp : max  \n\t\t\t\t\t} else {\n\t\t\t\t\t\tstack.removeAll()\n\t\t\t\t\t\ttmp = 0\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (stack.count == 0) {\n\t\t\t\treturn max\n\t\t\t} else {\n\t\t\t\tlet a = _max(Array(string[0..<lastContinuousIndex]))\n\t\t\t\tlet b =  _max(reverse(Array(string[lastContinuousIndex..<string.count])))\n\t\t\t\treturn a > b ? a : b\n\t\t\t}\n\t\t}\n\t\treturn _max(string)\n    }\n\n}\n```\n\nLet me know in comments if you have any doubts. I will be happy to answer.\n\nPlease upvote if you found the solution useful.
['Swift']