forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
53 lines (40 loc) · 1.3 KB
/
main2.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
/// Source : https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
/// Author : liuyubobobo
/// Time : 2017-11-14
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
// Sliding Window
// Another Implementation
//
// Time Complexity: O(len(s))
// Space Complexity: O(len(charset))
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int freq[256] = {0};
int l = 0, r = -1; // sliding window: s[l...r]
int res = 0;
while(r + 1 < s.size()){
while(r + 1 < s.size() && freq[s[r + 1]] == 0)
freq[s[++r]] ++;
res = max(res, r - l + 1);
if(r + 1 < s.size()){
freq[s[++r]] ++;
assert(freq[s[r]] == 2);
while(l <= r && freq[s[r]] == 2)
freq[s[l++]] --;
}
}
return res;
}
};
int main() {
cout << Solution().lengthOfLongestSubstring( "abcabcbb" )<<endl; //3
cout << Solution().lengthOfLongestSubstring( "bbbbb" )<<endl; //1
cout << Solution().lengthOfLongestSubstring( "pwwkew" )<<endl; //3
cout << Solution().lengthOfLongestSubstring( "c" )<<endl; //1
cout << Solution().lengthOfLongestSubstring( "" )<<endl; //0
return 0;
}