Here is the interview question prompt, presented for reference.
As a software engineer, you'll often be asked to optimize programs. One of the easiest ways to do so is by the introduction of an additional data structure.
Here's another classic problem along the same vein. We're provided a string like the following: "{[])}"
that is inclusive of the following symbols:
Can you write a function that will check if the symbol pairings in the string follow these below conditions?
For example, ()
is valid. ((
is not. Similarly, {{[]}}
is valid. [[}}
is not.
100000
[
,]
,{
,}
,(
,)
O(n)
O(n)
You can see the full challenge with visuals at this link.
Challenges • Asked almost 7 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Balanced Brackets.
Hello
Just want to mention that the code seems to have issues, it dosent pass all the test cases in leetcode, here is how i made it work
class Solution:
def isValid(self, s: str) -> bool:
mystk = []
paren = {'[': ']',
'(': ')',
'{': '}'
}
openpar = 0
for item in s:
if item in paren:
mystk.append(item)
openpar += 1
elif item in paren.values() and openpar == 0:
return False
else:
close = mystk.pop()
if paren[close] != item:
return False
openpar -= 1
if len(mystk) == 0:
return True
else:
return False