@@ -0,0 +1,1282 @@
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0A/**%0A * https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/%0A */%0Apublic class Solution %7B%0A    public int lengthOfLongestSubstringTwoDistinct(String s) %7B%0A        Map%3CCharacter, Integer%3E counter = new HashMap%3CCharacter, Integer%3E();%0A        int maxLength = 0, j = 0, numUnique = 0;%0A        for (int i = 0; i %3C s.length(); i++) %7B%0A            char ch = s.charAt(i);%0A            Integer c = counter.get(ch);%0A            if (c == null %7C%7C c == 0) %7B%0A                c = 1;%0A                numUnique++;%0A            %7D else %7B%0A                c++;%0A            %7D%0A            counter.put(ch, c);%0A%0A            while (numUnique %3E 2) %7B%0A                int c1 = counter.get(s.charAt(j));%0A                if (c1 == 1) %7B%0A                    numUnique--;%0A                %7D%0A                counter.put(s.charAt(j), c1 - 1);%0A                j++;%0A            %7D%0A            maxLength = Math.max(i - j + 1, maxLength);%0A        %7D%0A        return maxLength;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Solution solution = new Solution();%0A        System.out.println(solution.lengthOfLongestSubstringTwoDistinct(%22abaac%22));%0A        System.out.println(solution.lengthOfLongestSubstringTwoDistinct(%22abcabcabc%22));%0A    %7D%0A%7D%0A

add longest-substring-with-at-most-two-distinct-characters