✅ 1.
Valid Parentheses (LeetCode 20)
Problem: Check if a string of ()[]{} is valid.
def isValid(s):
stack = []
mapping = {')': '(', '}': '{', ']': '['}
for char in s:
if char in [Link]():
[Link](char)
elif char in mapping:
if not stack or [Link]() != mapping[char]:
return False
return not stack
✅ 2. Min Stack (LeetCode 155)
Problem: Design a stack that supports push , pop , top , and retrieving the minimum element.
class MinStack:
def __init__(self):
[Link] = []
self.min_stack = []
def push(self, val):
[Link](val)
min_val = val if not self.min_stack else min(val, self.min_stack[-1])
self.min_stack.append(min_val)
def pop(self):
[Link]()
self.min_stack.pop()
def top(self):
return [Link][-1]
def getMin(self):
return self.min_stack[-1]
✅ 3. Implement Stack using Queues (LeetCode 225)
Problem: Implement a stack using two queues.
from collections import deque
class MyStack:
def __init__(self):
self.q = deque()
def push(self, x):
[Link](x)
for _ in range(len(self.q) - 1):
[Link]([Link]())
def pop(self):
return [Link]()
def top(self):
return self.q[0]
def empty(self):
return not self.q
✅ 4. Remove All Adjacent Duplicates (LeetCode 1047)
Problem: Remove adjacent duplicates in a string.
def removeDuplicates(s):
stack = []
for char in s:
if stack and stack[-1] == char:
[Link]()
else:
[Link](char)
return ''.join(stack)
✅ 5. Baseball Game (LeetCode 682)
Problem: Simulate baseball game scoring using a stack.
def calPoints(ops):
stack = []
for op in ops:
if op == '+':
[Link](stack[-1] + stack[-2])
elif op == 'D':
[Link](2 * stack[-1])
elif op == 'C':
[Link]()
else:
[Link](int(op))
return sum(stack)
✅ 6. Final Prices With Discount (LeetCode 1475)
Problem: For each item, find the next item's price that is less or equal, and subtract it.
def finalPrices(prices):
stack = []
for i in range(len(prices)):
while stack and prices[stack[-1]] >= prices[i]:
j = [Link]()
prices[j] -= prices[i]
[Link](i)
return prices
✅ 7. Next Greater Element I (LeetCode 496)
Problem: Find the next greater element for each element in nums1 from nums2.
def nextGreaterElement(nums1, nums2):
stack, hashmap = [], {}
for num in nums2:
while stack and stack[-1] < num:
hashmap[[Link]()] = num
[Link](num)
return [[Link](num, -1) for num in nums1]
✅ 8. Backspace String Compare (LeetCode 844)
Problem: Check if two strings are equal after backspace # .
def build(s):
stack = []
for c in s:
if c != '#':
[Link](c)
elif stack:
[Link]()
return ''.join(stack)
def backspaceCompare(s, t):
return build(s) == build(t)
✅ 9. Evaluate Reverse Polish Notation (LeetCode 150)
Problem: Evaluate an expression in reverse Polish notation.
def evalRPN(tokens):
stack = []
for token in tokens:
if token not in "+-*/":
[Link](int(token))
else:
b, a = [Link](), [Link]()
if token == '+': [Link](a + b)
elif token == '-': [Link](a - b)
elif token == '*': [Link](a * b)
elif token == '/': [Link](int(a / b))
return stack[0]
✅ 10. Make The String Great (LeetCode 1544)
Problem: Remove adjacent characters if one is upper and other is lowercase of same letter.
def makeGood(s):
stack = []
for c in s:
if stack and abs(ord(stack[-1]) - ord(c)) == 32:
[Link]()
else:
[Link](c)
return ''.join(stack)