Decoding Nested Strings with Stacks

🔥Day 26 of #75DaysofLeetCode Cracked LeetCode 394 – Decode String (Medium) Today I solved a classic stack-based problem that really tests your understanding of nested structures! 💡 Problem Summary: Given an encoded string like 3[a2[c]], decode it. 👉 Output: accaccacc 🧠 Key Insight: Whenever you see nested brackets, think STACK. Because decoding happens inside-out ⤵️ a2[c] → acc → then 3[acc] → accaccacc ⚙️ Approach: ✔ Use two stacks: One for numbers (repeat counts) One for strings ✔ Traverse the string: Digit → build number [ → push current state ] → pop & decode Character → append 🚀 What I Learned: Stack is powerful for nested problems Always reset variables after pushing to stack Handle multi-digit numbers carefully (like 12[a]) ⏱️ Complexity: Time: O(n) Space: O(n) 📌 Takeaway: If the problem involves nested patterns → think stack immediately! #LeetCode #DSA #Java #CodingInterview #Stack #ProblemSolving #100DaysOfCode

  • text

To view or add a comment, sign in

Explore content categories