-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3.LongestSubstringWithoutRepeatingCharacters.h
More file actions
170 lines (122 loc) · 3.69 KB
/
3.LongestSubstringWithoutRepeatingCharacters.h
File metadata and controls
170 lines (122 loc) · 3.69 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
/*
bluepp
2014-06-12
2014-07-15
2014-09-06
2014-09-23
2014-10-21
2014-11-23
2014-12-08
2014-12-23
May the force be with me!
Problem: Longest Substring Without Repeating Characters
Source: https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/
Notes:
Given a string, find the length of the longest substring without repeating characters.
For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3.
For "bbbbb" the longest substring is "b", with the length of 1.
Solution: Pay attention when moving the 'start' pointer forward.
*/
/* 2016-12-31, update, hashtable */
int lengthOfLongestSubstring(string s) {
int n = s.length();
unordered_map<char, int> map;
int start = 0;
int ret = 0;
for (int i = 0; i < n; i++)
{
if (map.count(s[i]))
{
start = max(start, map[s[i]]+1);
}
map[s[i]] = i;
ret = max(ret, i-start+1);
}
return ret;
}
/* 2016-06-14, update another way */
/* https://leetcode.com/discuss/13336/shortest-o-n-dp-solution-with-explanations */
int lengthOfLongestSubstring(string s) {
int n = s.length();
vector<int> map(256, -1);
int start = 0;
int ret = 0;
for (int i = 0; i < n; i++)
{
start = max(start, map[s[i]]+1);
map[s[i]] = i;
ret = max(ret, i-start+1);
}
return ret;
}
/* 2015-08-27, update */
int lengthOfLongestSubstring(string s) {
int n = s.length();
if (n == 0) return 0;
unordered_map<char, int> map;
int ret = INT_MIN;
int start = 0, end = 0;
for (; end < n; end++)
{
if (map.count(s[end]))
{
ret = max(ret, end-start);
int t = map[s[end]];
for (int i = start; i <= t; i++)
{
map.erase(s[i]);
}
start = t+1;
}
map[s[end]] = end;
}
ret = max(ret, end-start);
return ret;
}
/* 2014-12-28 */
int lengthOfLongestSubstring(string s) {
int n = s.size();
bool exist[256];
memset(exist, false, sizeof(exist));
int ret = 0;
int start = 0, end = 0;
for (; end < n; end++)
{
if (exist[s[end]] == 0)
{
exist[s[end]] = true;
ret = max(ret, end-start+1);
}
else
{
for (; s[start] != s[end]; start++)
{
exist[s[start]] = false;
}
start ++;
}
}
return ret;
}
int lengthOfLongestSubstring(string s) {
bool exist[256];
memset(exist, false, sizeof(exist));
int count = 0;
int start = 0, end = 0;
int n = s.size();
while (end < n && start + count < n)
{
if (!exist[s[end]])
{
exist[s[end]] = true;
end ++;
}
else
{
exist[s[start]] = false;
start ++;
}
count = max(count, end-start);
}
return count;
}