-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_3_longestSubstringProblem.c
More file actions
51 lines (45 loc) · 1.23 KB
/
Copy path_3_longestSubstringProblem.c
File metadata and controls
51 lines (45 loc) · 1.23 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
#include <string.h>
#include <assert.h>
//https://leetcode.com/problems/longest-substring-without-repeating-characters/
// 20.12.23 - Vladislav Ershov
#define MAX(a, b) a >= b ? a : b
// return -1 if there was no match and index of symbol if match occurred
int notIn(const char* s, int len, char sym) {
for (int i = 0; i < len; ++i) {
if (s[i] == sym) return i;
}
return -1;
}
int lengthOfLongestSubstring(char* s) {
unsigned long len = strlen(s);
int max = len == 0 ? 0 : 1;
int startId = -1;
int currentLen = 0;
for (int i = 0; i < len; ++i) {
if(startId == -1) {
startId = i;
currentLen = 1;
}
else {
int match = notIn(&(s[startId]), currentLen, s[i]);
if (match == -1) {
currentLen++;
max = MAX(max, currentLen);
} else {
i = match + startId + 1;
startId += match + 1;
currentLen = 1;
}
}
}
return max;
}
#define TEST(s, res) assert(lengthOfLongestSubstring(s) == res);
int main() {
TEST("abcabcbb", 3);
TEST("bbbbb", 1);
TEST("pwwkew", 3);
TEST("", 0);
TEST("aab", 2);
TEST("dvdf", 3);
}