Stack — Last In, First Out
LIFO for matching, undo, and next-greater patterns.
Suno.Stringmeinbracketshain—(),[],{}.Kyaharopenkasahiclosehai,ordersahi?
Listen. String has brackets — (), [], {}. Does every open have a correct close in order?
0:00 / 0:58
When to use this
- Parentheses
- Monotonic stack
- Min-stack
C++ template
stack<char> st;
for (char c : s) {
if (/* open */) st.push(c);
else {
if (st.empty() || st.top() != need) return false;
st.pop();
}
}
return st.empty();Dry run
"()[]{}" → empty stack → true.Pitfalls
- Pop empty
- Wrong pair map
- Leftover opens
Say this in the interview
Push/pop O(1); full scan O(n).
Problems in this topic (3)
Each opens teach-first (animated when available), then the code panel.