Skip to content

Commit 0daeb2c

Browse files
committed
feat: add solutions to lc problem: No.0159
No.0159.Longest Substring with At Most Two Distinct Characters
1 parent 7eb222f commit 0daeb2c

File tree

6 files changed

+254
-4
lines changed

6 files changed

+254
-4
lines changed

solution/0100-0199/0159.Longest Substring with At Most Two Distinct Characters/README.md

+89-2
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,114 @@
2222
<strong>解释: <em>t</em></strong><em> </em>是 &quot;aabbb&quot;,长度为5。
2323
</pre>
2424

25-
2625
## 解法
2726

2827
<!-- 这里可写通用的实现逻辑 -->
2928

29+
哈希表 + 双指针。
30+
3031
<!-- tabs:start -->
3132

3233
### **Python3**
3334

3435
<!-- 这里可写当前语言的特殊实现逻辑 -->
3536

3637
```python
37-
38+
class Solution:
39+
def lengthOfLongestSubstringTwoDistinct(self, s: str) -> int:
40+
mp = Counter()
41+
i = j = ans = 0
42+
for c in s:
43+
mp[c] += 1
44+
while len(mp) > 2:
45+
mp[s[i]] -= 1
46+
if mp[s[i]] == 0:
47+
mp.pop(s[i])
48+
i += 1
49+
ans = max(ans, j - i + 1)
50+
j += 1
51+
return ans
3852
```
3953

4054
### **Java**
4155

4256
<!-- 这里可写当前语言的特殊实现逻辑 -->
4357

4458
```java
59+
class Solution {
60+
public int lengthOfLongestSubstringTwoDistinct(String s) {
61+
Map<Character, Integer> mp = new HashMap<>();
62+
int i = 0, j = 0, ans = 0;
63+
for (char c : s.toCharArray()) {
64+
mp.put(c, mp.getOrDefault(c, 0) + 1);
65+
while (mp.size() > 2) {
66+
char t = s.charAt(i);
67+
mp.put(t, mp.get(t) - 1);
68+
if (mp.get(t) == 0) {
69+
mp.remove(t);
70+
}
71+
++i;
72+
}
73+
ans = Math.max(ans, j - i + 1);
74+
++j;
75+
}
76+
return ans;
77+
}
78+
}
79+
```
80+
81+
### **C++**
82+
83+
```cpp
84+
class Solution {
85+
public:
86+
int lengthOfLongestSubstringTwoDistinct(string s) {
87+
unordered_map<char, int> mp;
88+
int i = 0, j = 0, ans = 0;
89+
for (char& c : s)
90+
{
91+
++mp[c];
92+
while (mp.size() > 2)
93+
{
94+
--mp[s[i]];
95+
if (mp[s[i]] == 0) mp.erase(s[i]);
96+
++i;
97+
}
98+
ans = max(ans, j - i + 1);
99+
++j;
100+
}
101+
return ans;
102+
}
103+
};
104+
```
45105
106+
### **Go**
107+
108+
```go
109+
func lengthOfLongestSubstringTwoDistinct(s string) int {
110+
mp := make(map[byte]int)
111+
i, j, ans := 0, 0, 0
112+
for _, c := range s {
113+
mp[byte(c)]++
114+
for len(mp) > 2 {
115+
mp[s[i]]--
116+
if mp[s[i]] == 0 {
117+
delete(mp, s[i])
118+
}
119+
i++
120+
}
121+
ans = max(ans, j-i+1)
122+
j++
123+
}
124+
return ans
125+
}
126+
127+
func max(a, b int) int {
128+
if a > b {
129+
return a
130+
}
131+
return b
132+
}
46133
```
47134

48135
### **...**

solution/0100-0199/0159.Longest Substring with At Most Two Distinct Characters/README_EN.md

+87-2
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,106 @@
3131
<li><code>s</code> consists of English letters.</li>
3232
</ul>
3333

34-
3534
## Solutions
3635

3736
<!-- tabs:start -->
3837

3938
### **Python3**
4039

4140
```python
42-
41+
class Solution:
42+
def lengthOfLongestSubstringTwoDistinct(self, s: str) -> int:
43+
mp = Counter()
44+
i = j = ans = 0
45+
for c in s:
46+
mp[c] += 1
47+
while len(mp) > 2:
48+
mp[s[i]] -= 1
49+
if mp[s[i]] == 0:
50+
mp.pop(s[i])
51+
i += 1
52+
ans = max(ans, j - i + 1)
53+
j += 1
54+
return ans
4355
```
4456

4557
### **Java**
4658

4759
```java
60+
class Solution {
61+
public int lengthOfLongestSubstringTwoDistinct(String s) {
62+
Map<Character, Integer> mp = new HashMap<>();
63+
int i = 0, j = 0, ans = 0;
64+
for (char c : s.toCharArray()) {
65+
mp.put(c, mp.getOrDefault(c, 0) + 1);
66+
while (mp.size() > 2) {
67+
char t = s.charAt(i);
68+
mp.put(t, mp.get(t) - 1);
69+
if (mp.get(t) == 0) {
70+
mp.remove(t);
71+
}
72+
++i;
73+
}
74+
ans = Math.max(ans, j - i + 1);
75+
++j;
76+
}
77+
return ans;
78+
}
79+
}
80+
```
81+
82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
int lengthOfLongestSubstringTwoDistinct(string s) {
88+
unordered_map<char, int> mp;
89+
int i = 0, j = 0, ans = 0;
90+
for (char& c : s)
91+
{
92+
++mp[c];
93+
while (mp.size() > 2)
94+
{
95+
--mp[s[i]];
96+
if (mp[s[i]] == 0) mp.erase(s[i]);
97+
++i;
98+
}
99+
ans = max(ans, j - i + 1);
100+
++j;
101+
}
102+
return ans;
103+
}
104+
};
105+
```
48106
107+
### **Go**
108+
109+
```go
110+
func lengthOfLongestSubstringTwoDistinct(s string) int {
111+
mp := make(map[byte]int)
112+
i, j, ans := 0, 0, 0
113+
for _, c := range s {
114+
mp[byte(c)]++
115+
for len(mp) > 2 {
116+
mp[s[i]]--
117+
if mp[s[i]] == 0 {
118+
delete(mp, s[i])
119+
}
120+
i++
121+
}
122+
ans = max(ans, j-i+1)
123+
j++
124+
}
125+
return ans
126+
}
127+
128+
func max(a, b int) int {
129+
if a > b {
130+
return a
131+
}
132+
return b
133+
}
49134
```
50135

51136
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int lengthOfLongestSubstringTwoDistinct(string s) {
4+
unordered_map<char, int> mp;
5+
int i = 0, j = 0, ans = 0;
6+
for (char& c : s)
7+
{
8+
++mp[c];
9+
while (mp.size() > 2)
10+
{
11+
--mp[s[i]];
12+
if (mp[s[i]] == 0) mp.erase(s[i]);
13+
++i;
14+
}
15+
ans = max(ans, j - i + 1);
16+
++j;
17+
}
18+
return ans;
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func lengthOfLongestSubstringTwoDistinct(s string) int {
2+
mp := make(map[byte]int)
3+
i, j, ans := 0, 0, 0
4+
for _, c := range s {
5+
mp[byte(c)]++
6+
for len(mp) > 2 {
7+
mp[s[i]]--
8+
if mp[s[i]] == 0 {
9+
delete(mp, s[i])
10+
}
11+
i++
12+
}
13+
ans = max(ans, j-i+1)
14+
j++
15+
}
16+
return ans
17+
}
18+
19+
func max(a, b int) int {
20+
if a > b {
21+
return a
22+
}
23+
return b
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int lengthOfLongestSubstringTwoDistinct(String s) {
3+
Map<Character, Integer> mp = new HashMap<>();
4+
int i = 0, j = 0, ans = 0;
5+
for (char c : s.toCharArray()) {
6+
mp.put(c, mp.getOrDefault(c, 0) + 1);
7+
while (mp.size() > 2) {
8+
char t = s.charAt(i);
9+
mp.put(t, mp.get(t) - 1);
10+
if (mp.get(t) == 0) {
11+
mp.remove(t);
12+
}
13+
++i;
14+
}
15+
ans = Math.max(ans, j - i + 1);
16+
++j;
17+
}
18+
return ans;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def lengthOfLongestSubstringTwoDistinct(self, s: str) -> int:
3+
mp = Counter()
4+
i = j = ans = 0
5+
for c in s:
6+
mp[c] += 1
7+
while len(mp) > 2:
8+
mp[s[i]] -= 1
9+
if mp[s[i]] == 0:
10+
mp.pop(s[i])
11+
i += 1
12+
ans = max(ans, j - i + 1)
13+
j += 1
14+
return ans

0 commit comments

Comments
 (0)