package com.fishercoder.solutions;

import java.util.HashMap;

/**
 * Given a string, find the length of the longest substring T that contains at most 2 distinct characters.

 For example, Given s = “eceba”,

 T is "ece" which its length is 3.
 */
public class _159 {

    public int lengthOfLongestSubstringTwoDistinct(String s) {
        if (s.length() < 1) {
            return 0;
        }
        HashMap<Character, Integer> index = new HashMap<Character, Integer>();
        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;
    }

}

@@ -59,15 +59,99 @@
 ap;%0A
-%0A/**%0A *
+import java.util.Map;%0A%0A/**%0A * 159. Longest Substring with At Most Two Distinct Characters%0A%0A
  Giv
@@ -161,16 +161,19 @@
 a string
+ s 
 , find t
@@ -207,17 +207,18 @@
 bstring 
-T
+t 
  that co
@@ -261,100 +261,245 @@
 .%0A%0A 
-For e
+E
 xample
-, Given s = %E2%80%9Ceceba%E2%80%9D,%0A%0A T is %22ece%22 which its length is 3.%0A */%0Apublic class _159 %7B%0A
+ 1:%0A%0A Input: %22eceba%22%0A Output: 3%0A Explanation: t is %22ece%22 which its length is 3.%0A%0A Example 2:%0A%0A Input: %22ccaabbb%22%0A Output: 5%0A Explanation: t is %22aabbb%22 which its length is 5.%0A */%0Apublic class _159 %7B%0A  public static class Solution1 %7B
 %0A   
@@ -560,26 +560,24 @@
  s) %7B%0A      
-  
 if (s.length
@@ -590,28 +590,24 @@
 ) %7B%0A        
- 
-   
 return 0;%0A  
@@ -610,26 +610,24 @@
 ;%0A      
-  
 %7D%0A      
   HashMa
@@ -622,14 +622,8 @@
     
- 
- Hash
 Map%3C
@@ -666,33 +666,13 @@
 Map%3C
-Character, Integer
 %3E();%0A
-  
     
@@ -687,26 +687,24 @@
  = 0;%0A      
-  
 int hi = 0;%0A
@@ -699,26 +699,24 @@
 int hi = 0;%0A
-  
       int ma
@@ -724,26 +724,24 @@
 Length = 0;%0A
-  
       while 
@@ -760,36 +760,32 @@
 th()) %7B%0A        
-    
 if (index.size()
@@ -795,38 +795,32 @@
  2) %7B%0A          
-      
 char c = s.charA
@@ -832,30 +832,24 @@
 ;%0A          
-  
-    
 index.put(c,
@@ -864,22 +864,16 @@
         
-      
 hi++;%0A  
@@ -874,34 +874,26 @@
 ++;%0A        
-   
- 
 %7D%0A
-    
         if (
@@ -922,22 +922,16 @@
         
-      
 int left
@@ -959,22 +959,16 @@
         
- 
-     
 for (int
@@ -990,24 +990,16 @@
 es()) %7B%0A
-        
         
@@ -1042,38 +1042,26 @@
 ;%0A          
-      %7D%0A      
+%7D%0A
           ch
@@ -1093,30 +1093,24 @@
 ;%0A          
-      
 index.remove
@@ -1114,22 +1114,16 @@
 ove(c);%0A
-      
         
@@ -1147,34 +1147,26 @@
  1;%0A        
-    %7D%0A    
+%7D%0A
         maxL
@@ -1205,30 +1205,26 @@
  lo);%0A      
-  %7D%0A  
+%7D%0A
       return
@@ -1241,11 +1241,14 @@
 ;%0A    %7D%0A
+  %7D
 %0A%7D%0A

refactor 159