Beat 85% Java slide window solution


  • 0
    public class Solution {
        public int lengthOfLongestSubstringTwoDistinct(String s) {
            int[] map = new int[128];
            int count = 0, start = 0, end = 0;
            int res = 0;
            while (end < s.length()) {
                if (map[s.charAt(end++)]++ == 0) count++;
                while (count > 2) if (map[s.charAt(start++)]-- == 1) count--;
                res = Math.max(res, end - start);
            }
            return res;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.