-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
41 lines (40 loc) · 917 Bytes
/
main.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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <vector>
using namespace std;
class Solution {
public:
/*
int lengthOfLongestSubstring(string s) {
int m[256] = {0}, res = 0, left = 0;
for(int i=0; i < s.size(); i++){
if(m[s[i]] == 0 || m[s[i]] < left){
res = max(res, i-left+1);
}else{
left = m[s[i]];
}
m[s[i]] = i + 1;
}
return res;
}
*/
int lengthOfLongestSubstring(string s) {
vector<int> m(256, -1);
int res = 0, left = -1;
for(int i=0; i<s.size(); i++){
left = max(left, m[s[i]]);
m[s[i]] = i;
res = max(res, i-left);
}
return res;
}
};
int main()
{
Solution s;
cout << s.lengthOfLongestSubstring("pwwkew") << endl;
return 0;
}