-
Notifications
You must be signed in to change notification settings - Fork 160
/
LongestSubstring2uniquechars.cpp
79 lines (52 loc) · 1.74 KB
/
LongestSubstring2uniquechars.cpp
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
/*
Given a string, find the longest substring containing just two unique chars.
*/
/*
solution: use two pointers as a sliding window to scan the string, use a map record the number of unique chars in the
substring. Update the record in the map when the second pointer increases, erase the record in the map when
the first pointer increases. Update and record the start position and length of the substring.
O(n) time, O(n) space
*/
#include<iostream>
#include<unordered_map>
#include<string>
using namespace std;
string LongestSubstringWithTwoUniqueChars(string &s) {
if(s.size() <= 2) return s;
size_t max_start = 0;
size_t max_len = 2;
size_t i = 0; //begin of a substring
size_t j = 0; //end of a substring
unordered_map<char, int> mp;
while (i <= j && j< s.size()) {
while (mp.size() <= 2 && j < s.size()) {
if (mp.count(s[j]) == 0) {
mp[s[j]] = 1;
} else {
++mp[s[j]];
}
if (mp.size() <= 2 && (j-i+1) > max_len) { //update the max_len if necessary
max_start = i;
max_len = j-i+1;
}
++j;
}
if (j == s.size()) return s.substr(max_start, max_len);
if (mp[s[i]] == 1) { //erase the record of s[i] in mp
mp.erase(s[i]);
} else {
--mp[s[i]];
}
++i;
}
return s;
}
int main() {
string str1 = "abcd";
string str2 = "abbccd";
string str3 = "abbccddd";
cout<<LongestSubstringWithTwoUniqueChars(str1)<<endl;
cout<<LongestSubstringWithTwoUniqueChars(str2)<<endl;
cout<<LongestSubstringWithTwoUniqueChars(str3)<<endl;
return 0;
}