Skip to content

Commit e73d2a7

Browse files
committed
Add stack questions
1 parent 266ac3f commit e73d2a7

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def remove(s):
2+
stack = []
3+
for element in s:
4+
if stack and element == stack[-1]:
5+
stack.pop()
6+
else:
7+
stack.append(element)
8+
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def check(s):
2+
stack = []
3+
for element in s:
4+
if element == '(':
5+
stack.append(')')
6+
elif element == '[':
7+
stack.append(']')
8+
elif element == '{':
9+
stack.append('}')
10+
elif not stack or stack.pop() != element:
11+
return False
12+
return not stack
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# pushed = [1,2,3,4,5]
2+
# popped = [4,5,3,2,1]
3+
4+
# this sequence is possible
5+
6+
def verify(pushed, popped):
7+
i = 0
8+
stack = []
9+
for x in pushed:
10+
stack.append(x)
11+
while stack and i < len(popped) and stack[-1] == popped[i]:
12+
stack.pop()
13+
i += 1
14+
return i == len(popped)

0 commit comments

Comments
 (0)