Tags: "leetcode", "hashmap", "substring", "sliding-window", "two-pointers", access_time 1-min read

Edit this post on Github

Longest Substring With at Most Two Distinct Characters

Created: March 16, 2019 by [lek-tin]

Last updated: March 16, 2019

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.

Solution

class Solution {
    public int lengthOfLongestSubstringTwoDistinct(String s) {
        HashMap<Character, Integer> countMap = new HashMap<Character, Integer>();
        int left = 0;
        int max = 0;

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            countMap.put(c, countMap.getOrDefault(c, 0) + 1);

            while (countMap.size() > 2) {
                char leftChar = s.charAt(left);
                countMap.put(leftChar, countMap.getOrDefault(leftChar, 0) - 1);

                if (countMap.get(leftChar) == 0) {
                    countMap.remove(leftChar);
                }
                left++;
            }
            max = Math.max(max, i - left + 1);
        }

        return max;
    }
}

Longest Substring With at Most K Distinct Characters