<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: prasanth cherukuri</title>
    <description>The latest articles on Forem by prasanth cherukuri (@prasanth_cherukuri).</description>
    <link>https://forem.com/prasanth_cherukuri</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3814114%2F46ec60a5-8412-4867-928a-c112be036de9.jpg</url>
      <title>Forem: prasanth cherukuri</title>
      <link>https://forem.com/prasanth_cherukuri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/prasanth_cherukuri"/>
    <language>en</language>
    <item>
      <title>Solving a Hard Sliding Window Problem (Step-by-Step Breakdown)</title>
      <dc:creator>prasanth cherukuri</dc:creator>
      <pubDate>Mon, 09 Mar 2026 07:20:59 +0000</pubDate>
      <link>https://forem.com/prasanth_cherukuri/solving-a-hard-sliding-window-problem-step-by-step-breakdown-332n</link>
      <guid>https://forem.com/prasanth_cherukuri/solving-a-hard-sliding-window-problem-step-by-step-breakdown-332n</guid>
      <description>&lt;h1&gt;
  
  
  Hard Sliding Window Pattern – Minimum Window Substring (Thinking Process)
&lt;/h1&gt;

&lt;p&gt;Problem Description: Given two strings s and p. Find the smallest substring in s consisting of all the characters (including duplicates) of the string p. Return empty string in case no such substring is present. If there are multiple such substring of the same length found, return the one with the least starting index.&lt;/p&gt;

&lt;p&gt;A brute force method of checking &lt;strong&gt;all substrings&lt;/strong&gt; is obviously very expensive.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Initial Thought
&lt;/h3&gt;

&lt;p&gt;Since we are dealing with &lt;strong&gt;continuous substrings&lt;/strong&gt;, I immediately started thinking in terms of &lt;strong&gt;two pointers / sliding window technique&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Core Requirement
&lt;/h3&gt;

&lt;p&gt;For each window, we need to check whether it contains &lt;strong&gt;all the target characters including duplicates&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Expensive Check (Naive Approach)
&lt;/h3&gt;

&lt;p&gt;An easy way to check this is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create an array/map with &lt;strong&gt;character → frequency&lt;/strong&gt; of the target string&lt;/li&gt;
&lt;li&gt;For every window, check if the window contains all required characters with the required frequency&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But this check becomes &lt;strong&gt;expensive if done repeatedly&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  4. Key Idea
&lt;/h3&gt;

&lt;p&gt;Instead of checking the frequency array repeatedly, we &lt;strong&gt;modify the frequency array while traversing the window&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;At any point in time:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;freq &amp;gt; 0&lt;/strong&gt; → we still need more of that character&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;freq = 0&lt;/strong&gt; → requirement for that character is satisfied&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;freq &amp;lt; 0&lt;/strong&gt; → the window already contains extra occurrences of that character&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  5. How to Remove the Expensive Check
&lt;/h3&gt;

&lt;p&gt;To avoid repeated comparisons, we introduce a &lt;strong&gt;count variable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initialize &lt;strong&gt;count = length of target string&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Reduce the frequency of characters while &lt;strong&gt;expanding the window&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Reduce the &lt;strong&gt;count only if the frequency of that character was positive&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When &lt;strong&gt;count becomes 0&lt;/strong&gt;, it means the current window contains &lt;strong&gt;all required characters&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  6. Shrinking the Window
&lt;/h3&gt;

&lt;p&gt;But we are not done yet — the current window might &lt;strong&gt;still be shrinkable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Move the &lt;strong&gt;left pointer&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Increment the frequency of that character&lt;/li&gt;
&lt;li&gt;If the frequency becomes &lt;strong&gt;positive&lt;/strong&gt;, it means we removed a required character&lt;/li&gt;
&lt;li&gt;Set &lt;strong&gt;count = 1&lt;/strong&gt; and stop shrinking&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At each valid stage, keep track of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Minimum length subarray&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Start index of that subarray&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  7. Final Step
&lt;/h3&gt;

&lt;p&gt;Return the &lt;strong&gt;smallest valid subarray&lt;/strong&gt; found.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Optimisations
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Instead of repeatedly comparing frequency arrays, we maintain a &lt;strong&gt;single integer &lt;code&gt;count&lt;/code&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;The frequency array naturally handles &lt;strong&gt;both relevant and irrelevant characters&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;We only care about characters that belong to the &lt;strong&gt;target string&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A useful observation:&lt;/p&gt;

&lt;p&gt;Irrelevant characters will &lt;strong&gt;never make the frequency positive&lt;/strong&gt;, so they don't affect the validity check.&lt;/p&gt;




&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity:&lt;/strong&gt; O(n)&lt;br&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt; O(1)&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>java</category>
      <category>slidingwindow</category>
      <category>dsa</category>
    </item>
  </channel>
</rss>
