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)
| 6 | // that surrounds it and parcounter will be 0 if all open |
| 7 | // parenthesis are closed correctly. |
| 8 | func 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 | } |
no outgoing calls