LeetCode 159: Longest Substring with At Most Two Distinct Characters Solution

Master LeetCode problem 159 (Longest Substring with At Most Two Distinct Characters), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

159. Longest Substring with At Most Two Distinct Characters

Problem Explanation

Explanation

Given a string, we are to find the length of the longest substring with at most two distinct characters. We can solve this problem using a sliding window approach. We can maintain a hashmap to store the frequency of characters in the current window and keep track of the distinct characters. As we iterate through the string, we update the window boundaries and keep track of the longest substring with at most two distinct characters.

  1. Initialize a hashmap to store the frequency of characters and two pointers to define the window boundaries.
  2. Iterate through the string:
    • Update the frequency of the current character in the hashmap.
    • If the size of the hashmap exceeds 2, we shrink the window from the left until the size becomes 2.
    • Update the length of the longest substring.
  3. Return the length of the longest substring.

Time Complexity: O(N), where N is the length of the input string. Space Complexity: O(1) since the hashmap will have at most 3 characters.

Solution Code

class Solution {
    public int lengthOfLongestSubstringTwoDistinct(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        
        int left = 0, right = 0, maxLen = 0;
        Map<Character, Integer> map = new HashMap<>();
        
        while (right < s.length()) {
            map.put(s.charAt(right), map.getOrDefault(s.charAt(right), 0) + 1);
            while (map.size() > 2) {
                map.put(s.charAt(left), map.get(s.charAt(left)) - 1);
                if (map.get(s.charAt(left)) == 0) {
                    map.remove(s.charAt(left));
                }
                left++;
            }
            maxLen = Math.max(maxLen, right - left + 1);
            right++;
        }
        
        return maxLen;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 159 (Longest Substring with At Most Two Distinct Characters)?

This page provides optimized solutions for LeetCode problem 159 (Longest Substring with At Most Two Distinct Characters) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 159 (Longest Substring with At Most Two Distinct Characters)?

The time complexity for LeetCode 159 (Longest Substring with At Most Two Distinct Characters) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 159 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 159 in Java, C++, or Python.

Back to LeetCode Solutions