Skip to content

Commit 00f008c

Browse files
authored
Merge pull request #1451 from Hemav009/159
Added 159
2 parents f8db4c5 + 356b4b4 commit 00f008c

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
---
2+
id: longest-substring-with-at
3+
title: Longest Substring Given At
4+
sidebar_label: 0159-Longest Substring Given At
5+
tags:
6+
- Leet code
7+
description: "Solution to leetocde 159"
8+
---
9+
10+
### Problem Description
11+
12+
Given a string s , find the length of the longest substring t that contains at most 2 distinct characters.
13+
14+
### Examples
15+
16+
**Example 1:**
17+
18+
```
19+
Input: "eceba"
20+
Output: 3
21+
Explanation: t is "ece" which its length is 3.
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: "ccaabbb"
28+
Output: 5
29+
Explanation: t is "aabbb" which its length is 5.
30+
```
31+
32+
Certainly! Below is the complete algorithm with explanations, followed by the implementations in C++, Python, Java, and JavaScript.
33+
34+
### Algorithm
35+
36+
1. **Initialize**:
37+
38+
- A hash map to count characters in the current window.
39+
- Two pointers, `i` and `j`, to represent the current window. `i` is the right pointer, and `j` is the left pointer.
40+
- A variable to keep track of the maximum length of the substring.
41+
42+
2. **Expand the Window**:
43+
44+
- Move the right pointer `i` to expand the window.
45+
- Add the current character to the hash map and update its count.
46+
47+
3. **Shrink the Window if Necessary**:
48+
49+
- If the number of distinct characters in the hash map exceeds 2, move the left pointer `j` to shrink the window until the number of distinct characters is at most 2.
50+
- Decrease the count of the character at the left pointer `j` in the hash map and remove it if its count becomes 0.
51+
52+
4. **Update the Maximum Length**:
53+
54+
- Update the maximum length of the substring after adjusting the window.
55+
56+
5. **Return the Result**:
57+
- Return the maximum length of the substring with at most two distinct characters.
58+
59+
### C++
60+
61+
```cpp
62+
#include <unordered_map>
63+
#include <string>
64+
#include <algorithm>
65+
66+
using namespace std;
67+
68+
class Solution {
69+
public:
70+
int lengthOfLongestSubstringTwoDistinct(string s) {
71+
unordered_map<char, int> cnt;
72+
int n = s.size();
73+
int ans = 0;
74+
int j = 0;
75+
76+
for (int i = 0; i < n; ++i) {
77+
cnt[s[i]]++;
78+
while (cnt.size() > 2) {
79+
cnt[s[j]]--;
80+
if (cnt[s[j]] == 0) {
81+
cnt.erase(s[j]);
82+
}
83+
++j;
84+
}
85+
ans = max(ans, i - j + 1);
86+
}
87+
88+
return ans;
89+
}
90+
};
91+
```
92+
93+
### Python
94+
95+
```python
96+
class Solution:
97+
def lengthOfLongestSubstringTwoDistinct(self, s: str) -> int:
98+
from collections import defaultdict
99+
100+
cnt = defaultdict(int)
101+
n = len(s)
102+
ans = 0
103+
j = 0
104+
105+
for i in range(n):
106+
cnt[s[i]] += 1
107+
while len(cnt) > 2:
108+
cnt[s[j]] -= 1
109+
if cnt[s[j]] == 0:
110+
del cnt[s[j]]
111+
j += 1
112+
ans = max(ans, i - j + 1)
113+
114+
return ans
115+
```
116+
117+
### Java
118+
119+
```java
120+
import java.util.HashMap;
121+
122+
public class Solution {
123+
public int lengthOfLongestSubstringTwoDistinct(String s) {
124+
HashMap<Character, Integer> cnt = new HashMap<>();
125+
int n = s.length();
126+
int ans = 0;
127+
int j = 0;
128+
129+
for (int i = 0; i < n; ++i) {
130+
cnt.put(s.charAt(i), cnt.getOrDefault(s.charAt(i), 0) + 1);
131+
while (cnt.size() > 2) {
132+
cnt.put(s.charAt(j), cnt.get(s.charAt(j)) - 1);
133+
if (cnt.get(s.charAt(j)) == 0) {
134+
cnt.remove(s.charAt(j));
135+
}
136+
++j;
137+
}
138+
ans = Math.max(ans, i - j + 1);
139+
}
140+
141+
return ans;
142+
}
143+
}
144+
```
145+
146+
### JavaScript
147+
148+
```javascript
149+
var lengthOfLongestSubstringTwoDistinct = function (s) {
150+
let cnt = new Map();
151+
let n = s.length;
152+
let ans = 0;
153+
let j = 0;
154+
155+
for (let i = 0; i < n; ++i) {
156+
cnt.set(s[i], (cnt.get(s[i]) || 0) + 1);
157+
while (cnt.size > 2) {
158+
cnt.set(s[j], cnt.get(s[j]) - 1);
159+
if (cnt.get(s[j]) === 0) {
160+
cnt.delete(s[j]);
161+
}
162+
++j;
163+
}
164+
ans = Math.max(ans, i - j + 1);
165+
}
166+
167+
return ans;
168+
};
169+
```

0 commit comments

Comments
 (0)