159. Longest Substring with At Most Two Distinct Characters

Description

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.

Problem URL


Solution

找到最长的最多有两个字母组成的子字符串长度。

We use two pointers to store the firsr position of a new character. If we meet the third character, we count the length then assign second to first and second to -1 then start iteration at first position.

Code

class Solution {
    public int lengthOfLongestSubstringTwoDistinct(String s) {
        if (s == null || s.length() == 0){
            return 0;
        }
        int count = 0, first = -1, second = -1;
        for (int i = 0; i < s.length(); i++){
            if (first == -1){
                first = i;
            }
            if (second == -1 && s.charAt(i) != s.charAt(first)){
                second = i;
            }
            if ( s.charAt(i) != s.charAt(first) &&  s.charAt(i) != s.charAt(second)){
                count = Math.max(count, i - first);
                first = second;
                second = -1;
                i = first;
            }            
        }
        return Math.max(count, s.length() - first);
    }
}

Time Complexity: O(n)
Space Complexity: O(1)


Review

Another template approach is using hash map to store substring’s characters. If third character is puted into the map. Calculate the first postion and calculate length then remove it

class Solution {
    public int lengthOfLongestSubstringTwoDistinct(String s) {
        if (s == null || s.length() == 0){
            return 0;
        }
        Map<Character, Integer> map = new HashMap<>();
        int low = 0, high = 0, res = Integer.MIN_VALUE;
        while (high < s.length()){
            if (map.size() <= 2){
                char ch = s.charAt(high);
                map.put(ch, high);
                high++;
            }
            if (map.size() > 2){
                int leftMost = s.length();
                for (int i : map.values()){
                    leftMost = Math.min(leftMost, i);
                }
                char ch = s.charAt(leftMost);
                map.remove(ch);
                low = leftMost + 1;
            }
            res = Math.max(res, high - low);
        }
        return res;
    }
}

你可能感兴趣的:(Leetcode,Hard)