-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongest_valid_par.go
54 lines (46 loc) · 986 Bytes
/
longest_valid_par.go
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
45
46
47
48
49
50
51
52
53
54
package problem0032
/*
Given a string containing just the characters '(' and ')', return the length of the longest valid (well-formed) parentheses substring.
*/
func longestValidParentheses(s string) int {
var pos = Stack{-1}
var res int
for i := range s {
if s[i] == '(' {
// Pushing the open parenthesis into the stack
pos.Push(i)
} else {
// Popping the last open parenthesis
pos.Pop()
if pos.Empty() {
// If stack is empty, we had no matching, push for next time
pos.Push(i)
} else {
// If stack is not empty, we had a matching, calc if this the longest
res = max(res, i-pos.Top())
}
}
}
return res
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
type Stack []int
func (s *Stack) Push(x int) {
*s = append(*s, x)
}
func (s *Stack) Pop() int {
r := (*s)[len(*s)-1]
*s = (*s)[:len(*s)-1]
return r
}
func (s *Stack) Top() int {
return (*s)[len(*s)-1]
}
func (s *Stack) Empty() bool {
return len(*s) == 0
}