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;
    }
  }
}

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;
        }
    }
}

refactor 159