-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_Q3_LongestSubString_SlidingWindow.cpp
More file actions
43 lines (41 loc) · 1.17 KB
/
LeetCode_Q3_LongestSubString_SlidingWindow.cpp
File metadata and controls
43 lines (41 loc) · 1.17 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
#include <iostream>
#include <map>
using namespace std;
int main(){
string s = "pwwkew";
int longestLength = 1;
if(s.length() == 0){return 0;}
int start = 0,end = 1;
int length = 1;
map<char,int> str;
str[s[start]] = start;
while (start <s.length()-1 && end < s.length()){
//cout <<end<< s[end] <<' ' << str.count(s[end])<< endl;
// while(str.count(s[end])<1){
// ++length;
// str[s[end]] = end;
// ++end;
// }
// if (length > longestLength)
// longestLength = length;
//++length;
//cout << 'c' << length << endl;
while(str.count(s[end])>0){
// cout << "erase " <<start<< s[start] <<endl;
str.erase(s[start]);
--length;
++start;
// cout << 'b' << length<<endl;
}
if(str.count(s[end])<1){
++length;
str[s[end]] = end;
++end;
// cout << 'a' << length<<endl;
}
if (length > longestLength)
longestLength = length;
}
cout << longestLength;
return 0;
}