...

/

Solution Review: Longest Continuous Balanced Parenthesis

Solution Review: Longest Continuous Balanced Parenthesis

Let's review the solution of the longest continuous balanced parenthesis problem.

We'll cover the following...

Solution

We keep track of the longest balanced parenthesis in the stack and update the length variable if we find a sequence greater than the previous one.

Press + to interact
main.go
stack.go
package main
import "fmt"
func longestContBalParen(str string, size int) int {
stk := new(Stack)
stk.Push(-1)
length := 0
for i := 0; i < size; i++ {
if (str[i] == '(') {
stk.Push(i)
} else // string[i] == ')'
{
stk.Pop()
if (stk.Length() != 0) {
if length < i - stk.Top(){
length = i - stk.Top()
}
} else {
stk.Push(i)
}
}
}
return length
}
// Testing code
func main() {
expn := "())((()))(())()(()"
size := len(expn)
value :=longestContBalParen(expn, size)
fmt.Println("longestContBalParen :: " , value)
}

Time complexity

The time complexity of this solution is ...