-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path159.LongestSubstringwithAtMostTwoDistinctCharacters.h
More file actions
210 lines (161 loc) · 4.83 KB
/
159.LongestSubstringwithAtMostTwoDistinctCharacters.h
File metadata and controls
210 lines (161 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
2015-06-10
2015-08-27, update linear space, version
bluepp
May the force be with me!
Longest Substring with At Most Two Distinct Characters Total Accepted: 2704 Total Submissions: 8893 My Submissions Question Solution
Given a string, find the length of the longest substring T that contains at most 2 distinct characters.
For example, Given s = “eceba”,
T is "ece" which its length is 3.
https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/
*/
/* 2016-12-31, update, hashtable */
int lengthOfLongestSubstringTwoDistinct(string s) {
int ret = 0;
unordered_map<char, int> map;
int start = 0;
for (int i = 0; i < s.length(); i++)
{
map[s[i]]++;
while (map.size() > 2)
{
map[s[start]]--;
if (map[s[start]] == 0)
{
map.erase(s[start]);
}
start ++;
}
ret = max(ret, i-start+1);
}
return ret;
}
/* 2015-08-27, linear space, update */
/* 2016-06-14, update */
int lengthOfLongestSubstringTwoDistinct(string s) {
int n = s.length();
char first = '0', second = '0';
int f_last = -1, s_last = -1;
int ret = 0;
int start = 0;
for (int i = 0; i < n; i++)
{
if (first == '0')
{
first = s[i];
f_last = i;
}
else if (s[i] == first)
{
f_last = i;
}
else if (second == '0')
{
second = s[i];
s_last = i;
}
else if (s[i] == second)
{
s_last = i;
}
else
{
if (f_last < s_last)
{
first = s[i];
start = f_last+1;
f_last = i;
}
else
{
second = s[i];
start = s_last +1;
s_last = i;
}
}
ret = max(ret, i-start+1);
}
return ret;
}
/* 2015-09-07, hash */
int lengthOfLongestSubstringTwoDistinct(string s) {
int n = s.length();
unordered_map<char, int> map;
int ret = 0;
int start = 0;
char first = '0', second = '0';
for (int i = 0; i < n; i++)
{
if (first == '0')
{
first = s[i];
}
else if (s[i] != first && second == '0')
{
second = s[i];
}
else if (s[i] != first && s[i] != second)
{
if (map[first] < map[second])
{
start = map[first]+1;
first = second;
}
else
{
start = map[second]+1;
}
second = s[i];
}
map[s[i]] = i;
ret = max(ret, i-start+1);
}
ret = max(ret, n-start);
return ret;
}
/* 2015-07-22, update, my version */
int lengthOfLongestSubstringTwoDistinct(string s) {
int n = s.length();
if (n < 2) return n;
int ret = 0;
unordered_map<char, int> map;
for (int start = 0, end = 0; end < n; end++)
{
map[s[end]]++;
while (map.size() > 2)
{
map[s[start]]--;
if (map[s[start]] == 0) map.erase(s[start]);
start++;
}
ret = max(ret, end-start+1);
}
return ret;
}
int lengthOfLongestSubstringTwoDistinct(string s) {
int n = s.length();
if (n <= 2) return n;
char first = s[0], second = '0';
int start = 0;
unordered_map<char, int> map;
int ret = 0;
for (int i = 1; i < n; i++)
{
if ((s[i] != first) && (s[i] != second))
{
if (second == '0')
{
second = s[i];
}
else
{
start = min(map[first], map[second]) + 1;
first = (map[first] < map[second]) ? second : first;
second = s[i];
}
}
map[s[i]] = i;
ret = max(ret, i-start+1);
}
return ret;
}