Stack-Based String Problems – Intuition Cheat Sheet

paras jainparas jain
3 min read

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 YourselfIf 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!

0
Subscribe to my newsletter

Read articles from paras jain directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

paras jain
paras jain