Skip to main content

Stack Problem - Balancing of Symbols

Stack Problem - Balancing of Symbols
Given an expression string exp , write a program to examine whether the pairs and the orders of “{“,”}”,”(“,”)”,”[“,”]” are correct in exp.

Input: exp = “[()]{}{[()()]()}”
Output: Balanced
Input: exp = “[(])”
Output: Not Balanced

  • Create a stack.
  • while ( end of input is not reached ) {
    • If the character read is not a symbol to be balanced, ignore it.
    • If the character is an opening delimiter like ( , {  or [ , PUSH it into the stack.
    • If it is a closing symbol like ) , } , ] , then if the stack is empty report an error, otherwise POP the stack.
    • If the symbol POP-ed is not the corresponding delimiter, report an error.
  • At the end of the input, if the stack is not empty report an error.
Implementation :
func balancingOfSymbols(string:String)->Bool{
        guard string.count > 0 else { return false }
        let symbols:[Character:Character] = ["{":"}","[":"]","(":")",]
        var symbolStack:Stack<Character> = Stack()
        var isBalanced = true
        for char in string  {
            if symbols.keys.contains(char){
            }else if symbols.values.contains(char){
                if let poppedSymbol = symbolStack.pop(), symbols[poppedSymbol] == char{
                     isBalanced = false
        isBalanced = isBalanced ? symbolStack.count == 0 : isBalanced
        return isBalanced
Time Complexity: O(n)

In case if you are wondering, how to implement a STACK, Please click here.


Popular posts from this blog

UILabel text animation as in UIButton click

Animating UILabel textcolor is not easy as it seems, normal animation won't work on this property. The below code snippet animates the UILabel textcolor as in button click.
Change the duration and UIViewAnimationOptions to play with it. extensionUILabel{ func flashLabel() { func animate(duration:Double,alpha:CGFloat,completion:(()->())?){ UIView.transition(with: self, duration: duration, options: UIViewAnimationOptions.transitionCrossDissolve, animations: { self.textColor = self.textColor?.withAlphaComponent(alpha) }) { _ in completion?() } } animate(duration: 0.1, alpha: 0) { animate(duration: 0.3, alpha: 1, completion: nil) } } } Happy coding...

UIAlertController made easy

Once I read somewhere on the internet, creating an Alert in IOS is comparatively harder than JavaScript. I'm not a big fan of HTML & CSS, but I do know a little Javascript, creating an Alert popup in Javascript is pretty much easier, just like this. alert("I am an alert box!"); That's all it takes to create an alert using JS. If you want to try other types of popups using JS, check out here.
But, let's try with Swift. let alertVC = UIAlertController(title: "Alert", message: "Testing", preferredStyle: .alert) let okAction = UIAlertAction(title: "OK", style: .default, handler: nil) alertVC.addAction(okAction) Looking at the above, IOS needs a little extra code, doesn't it? But, that's not the end, let's make it lesser, reusable and better. How about this? alert(title: "Alert", message: "Auto dismiss in 4 sec") I've written an extension for UIViewController, which uses just the above…