MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / Parenthesis

Function Parenthesis

strings/parenthesis/parenthesis.go:8–23  ·  view source on GitHub ↗

Parenthesis algorithm checks if every opened parenthesis is closed correctly. When parcounter is less than 0 when a closing parenthesis is detected without an opening parenthesis that surrounds it and parcounter will be 0 if all open parenthesis are closed correctly.

(text string)

Source from the content-addressed store, hash-verified

6// that surrounds it and parcounter will be 0 if all open
7// parenthesis are closed correctly.
8func Parenthesis(text string) bool {
9 parcounter := 0
10
11 for _, r := range text {
12 switch r {
13 case '(':
14 parcounter++
15 case ')':
16 parcounter--
17 }
18 if parcounter < 0 {
19 return false
20 }
21 }
22 return parcounter == 0
23}

Callers 1

TestParenthesisFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestParenthesisFunction · 0.68