File tree 6 files changed +254
-4
lines changed
solution/0100-0199/0159.Longest Substring with At Most Two Distinct Characters
6 files changed +254
-4
lines changed Original file line number Diff line number Diff line change 22
22
<strong >解释: <em >t</em ></strong ><em > </em >是 " ; aabbb" ; ,长度为5。
23
23
</pre >
24
24
25
-
26
25
## 解法
27
26
28
27
<!-- 这里可写通用的实现逻辑 -->
29
28
29
+ 哈希表 + 双指针。
30
+
30
31
<!-- tabs:start -->
31
32
32
33
### ** Python3**
33
34
34
35
<!-- 这里可写当前语言的特殊实现逻辑 -->
35
36
36
37
``` 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
38
52
```
39
53
40
54
### ** Java**
41
55
42
56
<!-- 这里可写当前语言的特殊实现逻辑 -->
43
57
44
58
``` 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
+ ```
45
105
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
+ }
46
133
```
47
134
48
135
### ** ...**
Original file line number Diff line number Diff line change 31
31
<li><code>s</code> consists of English letters.</li>
32
32
</ul >
33
33
34
-
35
34
## Solutions
36
35
37
36
<!-- tabs:start -->
38
37
39
38
### ** Python3**
40
39
41
40
``` 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
43
55
```
44
56
45
57
### ** Java**
46
58
47
59
``` 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
+ ```
48
106
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
+ }
49
134
```
50
135
51
136
### ** ...**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments