Computer Science Question of the Day

What is the simplest and/or most efficient function you can write to determine whether a given string has balanced brackets or not?

Fun question for all the CS nerdz out there:

What is the simplest and/or most efficient function you can write to determine whether a given string has balanced brackets or not?

Examples:

  • Balanced: “(This is balanced)”
  • Not Balanced: “(((This is *not*) balanced.”

Function prototype:

bool stringHasBalancedBrackets(string inputString)

The function should return true if the brackets are balanced, otherwise false.

(BTW, there is a great solution on Stack Overflow; I’m looking to see what everyone can come up with off the top of their heads.)