I am doing the Leetcode valid parenthesis question and want to implement the ans using deque stack. there are three types of parenthesis {[()]} and i need to check the top of the stack before i pop it.
i couldnt find any method for collections.deque similar to list stack peek()
from collections import deque
class Solution:
def isValid(self, s: str) -> bool:
stack=deque()
for i in range(len(s)):
if(s[i]=="{"):
stack.append("{")
elif(s[i]=="("):
stack.append("(")
elif(s[i]=="["):
stack.append("[")
if stack.maxlen() is not None:
if(s[i]=="}" and "{"==stack[-1]):
stack.pop()
elif(s[i]==")" and "("==stack[-1]):
stack.pop()
elif(s[i]=="]" and "["==stack[-1]):
stack.pop()
if(bool(stack)):
return False
else:
return True
i know this code doesn't work already and is incomplte, for example:
if stack.maxlen() is not None:
this cant work, i am still working on the syntax and logic.
i need a way to see the top of the stack in below part of code in the "if condition"
if(s[i]=="}" and "{"==stack[-1]):
stack.pop()