Skip to content

Commit 67bb6b1

Browse files
committed
LongestSubstringWithoutRepeatingCharacters
1 parent f4d3827 commit 67bb6b1

2 files changed

+74
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<stdio.h>
2+
#include<string.h>
3+
#include<stdlib.h>
4+
#include<stdbool.h>
5+
#include<math.h>
6+
int lengthOfLongestSubstring(char *s)
7+
{
8+
int head, tail, length, maxlength;
9+
head = tail = length = maxlength = 0;
10+
bool a[256];
11+
memset(a, 0, sizeof(a));
12+
for (; s[tail] != '\0'; ++ tail)
13+
{
14+
int num = (int)s[tail];
15+
if (a[num])
16+
{
17+
if (length > maxlength) maxlength = length;
18+
while (s[head ++] - s[tail])
19+
{
20+
a[(int)s[head - 1]] = 0;
21+
-- length;
22+
}
23+
} else {
24+
a[num] = 1;
25+
length ++;
26+
}
27+
}
28+
if (length > maxlength) maxlength = length;
29+
return maxlength;
30+
}
31+
int main()
32+
{
33+
FILE *fin = fopen("oo.xx", "r");
34+
FILE *fout = fopen("xx.oo", "w");
35+
char s[256];
36+
fscanf(fin, "%s", s);
37+
fprintf(fout, "%d\n", lengthOfLongestSubstring(s));
38+
return 0;
39+
}
40+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import functools as ft
2+
from functools import partial as par
3+
def F(*z):
4+
z = [*z]
5+
return [*ft.reduce(lambda x, y: map(y, x), [z[:1]] + z[1:])][0]
6+
FF = lambda *z: [*ft.reduce(lambda x, y: map(y, x), z)]
7+
fyx = lambda f, *x: lambda *y: f(*y, *x)
8+
class Solution:
9+
def lengthOfLongestSubstring(self, s: str) -> int:
10+
length = maxlength = 0
11+
check = []
12+
for x in s:
13+
if x in check:
14+
if length > maxlength:
15+
maxlength = length
16+
pos = check.index(x)
17+
length -= pos
18+
temp = check[:pos + 1]
19+
check = check[pos + 1:]
20+
del(temp)
21+
check.append(x)
22+
else:
23+
check.append(x)
24+
length += 1
25+
if (length > maxlength):
26+
maxlength = length
27+
return maxlength
28+
29+
fin = open("oo.xx", "r")
30+
fout = open("xx.oo", "w")
31+
32+
s = fin.readline()[:-1]
33+
F(s, Solution().lengthOfLongestSubstring, str, fout.write)
34+

0 commit comments

Comments
 (0)