package com.fishercoder.solutions;

import java.util.HashMap;
import java.util.Map;

/**
 * 159. Longest Substring with At Most Two Distinct Characters

 Given a string s , find the length of the longest substring t  that contains at most 2 distinct characters.

 Example 1:

 Input: "eceba"
 Output: 3
 Explanation: t is "ece" which its length is 3.

 Example 2:

 Input: "ccaabbb"
 Output: 5
 Explanation: t is "aabbb" which its length is 5.
 */
public class _159 {
  public static class Solution1 {
    public int lengthOfLongestSubstringTwoDistinct(String s) {
      if (s.length() < 1) {
        return 0;
      }
      Map<Character, Integer> index = new HashMap<>();
      int lo = 0;
      int hi = 0;
      int maxLength = 0;
      while (hi < s.length()) {
        if (index.size() <= 2) {
          char c = s.charAt(hi);
          index.put(c, hi);
          hi++;
        }
        if (index.size() > 2) {
          int leftMost = s.length();
          for (int i : index.values()) {
            leftMost = Math.min(leftMost, i);
          }
          char c = s.charAt(leftMost);
          index.remove(c);
          lo = leftMost + 1;
        }
        maxLength = Math.max(maxLength, hi - lo);
      }
      return maxLength;
    }
  }
}

@@ -145,17 +145,18 @@
 racters%0A
-%0A
+ *
  Given a
@@ -256,17 +256,21 @@
 acters.%0A
-%0A
+ *%0A *
  Example
@@ -273,17 +273,18 @@
 mple 1:%0A
-%0A
+ *
  Input: 
@@ -291,16 +291,18 @@
 %22eceba%22%0A
+ *
  Output:
@@ -304,16 +304,18 @@
 tput: 3%0A
+ *
  Explana
@@ -354,17 +354,21 @@
 h is 3.%0A
-%0A
+ *%0A *
  Example
@@ -371,17 +371,18 @@
 mple 2:%0A
-%0A
+ *
  Input: 
@@ -391,16 +391,18 @@
 caabbb%22%0A
+ *
  Output:
@@ -404,16 +404,18 @@
 tput: 5%0A
+ *
  Explana
@@ -480,16 +480,18 @@
  _159 %7B%0A
+  
   public
@@ -516,16 +516,20 @@
 tion1 %7B%0A
+    
     publ
@@ -579,24 +579,30 @@
 String s) %7B%0A
+      
       if (s.
@@ -613,24 +613,32 @@
 th() %3C 1) %7B%0A
+        
         retu
@@ -645,26 +645,38 @@
 rn 0;%0A      
+ 
-%7D%0A
+     %7D%0A      
       Map%3CCh
@@ -720,24 +720,30 @@
 %3C%3E();%0A      
+    
+  
 int lo = 0;%0A
@@ -738,24 +738,30 @@
 int lo = 0;%0A
+      
       int hi
@@ -762,24 +762,30 @@
 int hi = 0;%0A
+      
       int ma
@@ -803,16 +803,22 @@
 ;%0A      
+ 
+     
 while (h
@@ -835,32 +835,40 @@
 th()) %7B%0A        
+   
+     
 if (index.size()
@@ -878,32 +878,42 @@
  2) %7B%0A          
+  
+        
 char c = s.charA
@@ -925,24 +925,34 @@
 ;%0A          
+       
+   
 index.put(c,
@@ -953,24 +953,34 @@
 put(c, hi);%0A
+          
           hi
@@ -983,34 +983,50 @@
   hi++;%0A        
+  
-%7D%0A
+      %7D%0A        
         if (inde
@@ -1047,24 +1047,34 @@
 %7B%0A          
+ 
+         
 int leftMost
@@ -1098,16 +1098,26 @@
         
+      
+    
 for (int
@@ -1151,16 +1151,28 @@
         
+           
+ 
 leftMost
@@ -1199,34 +1199,48 @@
 , i);%0A          
-%7D%0A
+          %7D%0A    
           char c
@@ -1225,32 +1225,38 @@
 %7D%0A              
+      
 char c = s.charA
@@ -1274,24 +1274,34 @@
 ;%0A          
+         
+ 
 index.remove
@@ -1315,16 +1315,26 @@
         
+       
+   
 lo = lef
@@ -1344,34 +1344,50 @@
 st + 1;%0A        
+    
-%7D%0A
+    %7D%0A        
         maxLengt
@@ -1422,26 +1422,38 @@
  lo);%0A      
+   
-%7D%0A
+   %7D%0A      
       return
@@ -1468,16 +1468,22 @@
 th;%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 159