package com.fishercoder.solutions;

/**
 * 76. Minimum Window Substring
 *
 * Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

 For example,
 S = "ADOBECODEBANC"
 T = "ABC"

 Minimum window is "BANC".

 Note:

 If there is no such window in S that covers all characters in T, return the empty string "".
 If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
 */

public class _76 {

  public static class Solution1 {
    public String minWindow(String s, String t) {
      int[] counts = new int[256];
      for (char c : t.toCharArray()) {
        counts[c]++;
      }

      int start = 0;
      int end = 0;
      int minStart = 0;
      int minLen = Integer.MAX_VALUE;
      int counter = t.length();
      while (end < s.length()) {
        if (counts[s.charAt(end)] > 0) {
          counter--;
        }

        counts[s.charAt(end)]--;
        end++;

        while (counter == 0) {
          if (end - start < minLen) {
            minStart = start;
            minLen = end - start;
          }
          counts[s.charAt(start)]++;
          if (counts[s.charAt(start)] > 0) {
            counter++;
          }
          start++;
        }
      }

      if (minLen == Integer.MAX_VALUE) {
        return "";
      }
      return s.substring(minStart, minStart + minLen);
    }
  }
}

@@ -33,477 +33,8 @@
 s;%0A%0A
-/**%0A * 76. Minimum Window Substring%0A *%0A * Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).%0A%0A For example,%0A S = %22ADOBECODEBANC%22%0A T = %22ABC%22%0A%0A Minimum window is %22BANC%22.%0A%0A Note:%0A%0A If there is no such window in S that covers all characters in T, return the empty string %22%22.%0A If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.%0A */%0A%0A
 publ
@@ -49,16 +49,18 @@
  _76 %7B%0A%0A
+  
   public
@@ -85,24 +85,28 @@
 tion1 %7B%0A    
+   
+ 
 public Strin
@@ -145,16 +145,22 @@
 %7B%0A      
+    
+  
 int%5B%5D co
@@ -186,16 +186,22 @@
 ;%0A      
+    
+  
 for (cha
@@ -229,24 +229,32 @@
 ) %7B%0A        
+    
+    
 counts%5Bc%5D++;
@@ -250,24 +250,30 @@
 ounts%5Bc%5D++;%0A
+      
       %7D%0A%0A   
@@ -271,24 +271,30 @@
    %7D%0A%0A      
+ 
+     
 int start = 
@@ -298,24 +298,30 @@
  = 0;%0A      
+ 
+     
 int end = 0;
@@ -323,24 +323,30 @@
  = 0;%0A      
+ 
+     
 int minStart
@@ -347,24 +347,30 @@
 nStart = 0;%0A
+      
       int mi
@@ -395,16 +395,22 @@
 _VALUE;%0A
+      
       in
@@ -435,24 +435,30 @@
 th();%0A      
+     
+ 
 while (end %3C
@@ -468,24 +468,32 @@
 length()) %7B%0A
+        
         if (
@@ -527,24 +527,34 @@
 %7B%0A          
+  
+        
 counter--;%0A 
@@ -560,27 +560,42 @@
         
+    
-%7D%0A%0A
+    %7D%0A%0A       
         
 counts%5Bs
@@ -578,32 +578,33 @@
 %0A               
+ 
 counts%5Bs.charAt(
@@ -608,24 +608,32 @@
 At(end)%5D--;%0A
+        
         end+
@@ -640,24 +640,32 @@
 +;%0A%0A        
+   
+     
 while (count
@@ -681,24 +681,34 @@
 %7B%0A          
+      
+    
 if (end - st
@@ -735,16 +735,28 @@
         
+        
+    
 minStart
@@ -777,16 +777,28 @@
         
+       
+     
 minLen =
@@ -813,34 +813,51 @@
 tart;%0A          
-%7D%0A
+          %7D%0A       
           counts
@@ -842,32 +842,35 @@
                 
+   
 counts%5Bs.charAt(
@@ -882,32 +882,42 @@
 )%5D++;%0A          
+         
+ 
 if (counts%5Bs.cha
@@ -943,24 +943,36 @@
             
+          
+  
 counter++;%0A 
@@ -966,24 +966,34 @@
  counter++;%0A
+          
           %7D%0A
@@ -984,32 +984,42 @@
               %7D%0A
+          
           start+
@@ -1033,19 +1033,39 @@
     
+ 
-%7D%0A      %7D%0A%0A
+       %7D%0A            %7D%0A%0A      
     
@@ -1105,24 +1105,32 @@
 ) %7B%0A        
+    
+    
 return %22%22;%0A 
@@ -1130,26 +1130,38 @@
 n %22%22;%0A      
+    
-%7D%0A
+  %7D%0A      
       return
@@ -1207,16 +1207,22 @@
 n);%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 76