We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
funcisOpenerBracket(istring)bool{ifi=="("||i=="["||i=="{"{returntrue}returnfalse}funcremoveLastElement(s[]rune)[]rune{returns[:len(s)-1]}funcisBalanced(sstring)string{// Write your code heresRune:=[]rune(s)reverseBracket:=map[string]string{"(":")","{":"}","[":"]",}varstackS[]runeif!isOpenerBracket(string(s[0])){return"NO"}fori:=0;i<len(sRune);i++{ifisOpenerBracket(string(sRune[i])){stackS=append(stackS,sRune[i])}else{iflen(stackS)==0||reverseBracket[(string(stackS[len(stackS)-1]))]!=string(sRune[i]){return"NO"}stackS=removeLastElement(stackS)}}iflen(stackS)>0{return"NO"}return"YES"}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
This is my solution using Go, I hope it can help