Overview
Sliding window O(n) mein array/string ke contiguous subsequences solve karta hai.
Analogy
Jaise train window se scenery—ek portion observe karo aur smoothly aage badhao.
Step-by-step
- Window boundaries ke liye do pointers left aur right
- Right expand: right boundary right move karo
- Left contract: condition violate ho toh left boundary right move karo
- Har step mein answer update karo (maximum etc.)
Visual
"abcabcbb" longest non-repeating
→expand to a,b,c(3)
→left shift duplicate a remove karo
→b,c,a(3) → answer 3
Common mistakes
- Left shift par window data update nahi karna
- Sirf maximum window dhundna, minimum ignore karna
Practice questions
- Length k ka maximum consecutive sum nikalo
- Saare specified characters wali minimum substring dhundo