Nested Brackets Algorithm

Specific forms of the mark include rounded brackets (also named parenthesis), square brackets, curly brackets (also named braces), and angle brackets (also named chevrons), as well as various less common pairs of symbols. typically deployed in symmetric pairs, an individual bracket may be identify as a left or right bracket or, alternatively, an opening paired bracket or closing paired bracket, respectively, depending on the directionality of the context. Chevrons, ⟨ ⟩, were the earliest type of bracket to look in write English. Desiderius Erasmus coined the term lunula to refer to the rounded parenthesis, (), recalling the shape of the crescent moon.
// The nested brackets problem is a problem that determines if a sequence of
// brackets are properly nested.  A sequence of brackets s is considered properly nested
// if any of the following conditions are true:
// 	- s is empty
// 	- s has the form (U) or [U] or {U} where U is a properly nested string
// 	- s has the form VW where V and W are properly nested strings
// For example, the string "()()[()]" is properly nested but "[(()]" is not.
// The function called isBalanced takes as input a string which is a sequence of brackets and
// returns true if input is nested and false otherwise.

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func isBalanced(input string) string {

	if len(input) > 0 {
		var stack []byte
		for i := 0; i < len(input); i++ {
			if input[i] == '(' || input[i] == '{' || input[i] == '[' {
				stack = append(stack, input[i])
			} else {
				if len(stack) > 0 {
					pair := string(stack[len(stack)-1]) + string(input[i])
					stack = stack[:len(stack)-1]

					if pair != "[]" && pair != "{}" && pair != "()" {
						return input + " is not balanced."
					}
				} else {
					return input + " is not balanced."
				}
			}
		}
		if len(stack) == 0 {
			return input + " is balanced."
		}
	}
	return "Please enter a sequence of brackets."
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	fmt.Print("Enter sequence of brackets: ")
	text, _ := reader.ReadString('\n')

	text = strings.TrimSpace(text)
	fmt.Println(isBalanced(text))
}

LANGUAGE:

DARK MODE: