Gültige Klammern
class Solution:
def isValid(self, s):
if s == "":
return True
if len(s) < 2:
return False
pair_brkts = {
"{" : "}",
"(" : ")",
"[" : "]"
}
stack = []
for i in s:
if i in pair_brkts:
stack.append(i) #stack i(forward facing brackets) that also exists as keys in our pair_brkts dictionary)
#print("forward facing brackets", stack) #to see the contents of your stacked list
else:
if len(stack) == 0 or pair_brkts[stack.pop()] != i: #if stack list is empty or the value pair of last
#list item isnt same, return False, otherwise break out of loop
#print("backward facing brackets", stack) #print to see whats left of your list after
#looping is over for all your i(i.e brackets)
return False
if len(stack) > 0: #if stack list is not empty at this point, return False, else return True
return False
return True
Task = Solution()
print("1. ", Task.isValid("({[()]})"))
print("2. ", Task.isValid("()[]{}"))
print("3. ", Task.isValid("(]"))
Kingsley Atuba