Given a string s
, return the length of the longest substring that contains at most two distinct characters.
Example 1:
Input: s = "eceba" Output: 3 Explanation: The substring is "ece" which its length is 3.
Example 2:
Input: s = "ccaabbb" Output: 5 Explanation: The substring is "aabbb" which its length is 5.
Constraints:
1 <= s.length <= 105
s
consists of English letters.
class Solution:
def lengthOfLongestSubstringTwoDistinct(self, s: str) -> int:
mp = Counter()
i = j = ans = 0
for c in s:
mp[c] += 1
while len(mp) > 2:
mp[s[i]] -= 1
if mp[s[i]] == 0:
mp.pop(s[i])
i += 1
ans = max(ans, j - i + 1)
j += 1
return ans
class Solution {
public int lengthOfLongestSubstringTwoDistinct(String s) {
Map<Character, Integer> mp = new HashMap<>();
int i = 0, j = 0, ans = 0;
for (char c : s.toCharArray()) {
mp.put(c, mp.getOrDefault(c, 0) + 1);
while (mp.size() > 2) {
char t = s.charAt(i);
mp.put(t, mp.get(t) - 1);
if (mp.get(t) == 0) {
mp.remove(t);
}
++i;
}
ans = Math.max(ans, j - i + 1);
++j;
}
return ans;
}
}
class Solution {
public:
int lengthOfLongestSubstringTwoDistinct(string s) {
unordered_map<char, int> mp;
int i = 0, j = 0, ans = 0;
for (char& c : s) {
++mp[c];
while (mp.size() > 2) {
--mp[s[i]];
if (mp[s[i]] == 0) mp.erase(s[i]);
++i;
}
ans = max(ans, j - i + 1);
++j;
}
return ans;
}
};
func lengthOfLongestSubstringTwoDistinct(s string) int {
mp := make(map[byte]int)
i, j, ans := 0, 0, 0
for _, c := range s {
mp[byte(c)]++
for len(mp) > 2 {
mp[s[i]]--
if mp[s[i]] == 0 {
delete(mp, s[i])
}
i++
}
ans = max(ans, j-i+1)
j++
}
return ans
}
func max(a, b int) int {
if a > b {
return a
}
return b
}