Stack-Based String Problems β Intuition Cheat Sheet


When you're faced with tricky string problems that involve brackets, nested patterns, or context switching, there's a high chance a stack is your best friend. In this article, we break down the intuition behind stack-based string problems and give you a cheat sheet you can always come back to.
π§ Why Stack?
Stacks are perfect when you need a Last-In-First-Out (LIFO) structure. That means when you go into a nested structure or a new "context," you can push it onto the stack, and when you're done, pop it back out in the correct order.
π 1. Decode String
Example: "3[a2[c]]"
β "accaccacc"
π€ Problem:
You're given a string with numbers and brackets. The goal is to decode it.
π΅οΈβ Solution Intuition:
Use a count stack to store repeat numbers.
Use a string stack to store the previous string context.
When you hit ']', you pop the top count and string, repeat the current segment, and combine.
π 2. Valid Parentheses
Example: "({[]})"
β Valid
π Problem:
Check if every opening bracket has a matching closing bracket.
ποΈ Solution Intuition:
Push opening brackets to the stack.
On closing bracket, check if it matches the top of the stack.
If mismatched or stack is not empty at end, it's invalid.
π 3. Evaluate Reverse Polish Notation
Example: ["2", "1", "+", "3", "*"]
β (2 + 1) * 3 = 9
π Problem:
Evaluate a postfix expression.
βοΈ Solution Intuition:
Push numbers to the stack.
When an operator appears, pop the top two numbers, apply the operation, and push the result.
π 4. Simplify Unix-Style Path
Example: "/a/./b/../../c/"
β "/c"
π€ Problem:
Simplify a string representing a Unix-style path.
π§ Solution Intuition:
Split the path by '/' and use a stack.
If you see "..", pop from the stack.
If you see a valid folder, push it.
Join the stack with '/' at the end.
π 5. Remove All Adjacent Duplicates
Example: "abbaca"
β "ca"
β Problem:
Remove characters that appear consecutively.
π Solution Intuition:
Use a stack.
If the current character matches the top, pop it.
Else, push it.
π Bonus: Quick Self-Checklist Before Choosing Stack
Ask Yourself | If Yes, Use Stack? |
Is there nesting or reversal involved? | β Yes |
Do I need to backtrack or undo something? | β Yes |
Are characters being grouped contextually? | β Yes |
πΉ Common Patterns To Memorize
// Decode String
char = digit β store k
char = '[' β push k & current, reset current
char = letter β append to current
char = ']' β pop k & prev, repeat current and append
// Valid Parentheses
if opening bracket β push
if closing β match with top
// Postfix Eval
if number β push
if operator β pop 2, apply, push result
// Simplify Path
split by '/'
if ".." β pop
if valid folder β push
π Final Tip:
Practicing stack problems improves pattern recognition over time. Once you see brackets or nested structure, your brain will automatically say: "Stack time!"
Want to take it further? Try LeetCode problems like:
Decode String
Simplify Path
Valid Parentheses
Basic Calculator II
Remove All Adjacent Duplicates
Save this cheat sheet. Itβs your Stack Survival Kit βοΈ for string-based problems!
Subscribe to my newsletter
Read articles from paras jain directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
