-
Notifications
You must be signed in to change notification settings - Fork 28
/
Contents.swift
44 lines (35 loc) · 1016 Bytes
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import Foundation
/*
Given a string containing just the characters '(' and ')',
find the length of the longest valid (well-formed)
parentheses substring.
*/
//O(n) - time
//O(n) - space
func longestValidParentheses(in string: String) -> Int {
let array = Array(string)
if array.count < 2 { return 0 }
var history = Array(repeating: false, count: array.count)
var stack: [(char: Character, index: Int)] = []
for i in 0 ..< array.count {
if array[i] == "(" {
stack.append((char: array[i], index: i))
}
else if array[i] == ")",
let last = stack.last,
last.char == "(" {
history[last.index] = true
history[i] = true
stack.removeLast()
}
}
var max = 0
var count = 0
history.forEach { valid in
count = valid ? count + 1 : 0
if count > max { max = count }
}
return max
}
var str = "()(()"
longestValidParentheses(in: str)