Skip to main content

Posts

Showing posts with the label daily coding problem

Daily Coding Problem: Balanced parenthesis

My solution to a “Daily Coding Problem” that I received in my mail today. You’re given a string consisting solely of  ( ,  ) , and  * .  *  can represent either a  ( ,  ) , or an empty string. Determine whether the parentheses are balanced. For example,  (()*  and  (*)  are balanced.  )*(  is not balanced. Here’s my solution in Typescript, oneFortyTwo(str: string): boolean { if(str == undefined || str == null) { return false; } //not sure if this is right? if(str.length == 1){ return false; } let balanced: boolean = false; let parenStack = new Stack<string>(); for(let i=0; i < str.length; i++) { let char = str.charAt(i); let topChar = parenStack.peek(); if(topChar == null) { parenStack.push(char); } else { if(topChar == "*") { ...