-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxsub.c
51 lines (48 loc) · 891 Bytes
/
maxsub.c
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 <stdio.h>
#include <string.h>
#include <stdlib.h>
int lengthOfLongestSubstring(char* s) {
int i=0,j=0,max=1;
char *cnt = (char *)malloc(256*sizeof(char));
if(s[0]=='\0')
{
return 0;
}
while(*s!='\0')
{
i=0,j=0;
memset(cnt,0,256);
cnt[s[0]]=1;
while(s[j+1]!='\0')
{
if(cnt[s[j+1]]!=0)
{
max = j-i+1>max?j-i+1:max;
memset(cnt,0,256);
j=j+1;
i=j;
cnt[s[j]]=1;
}
else
{
j=j+1;
cnt[s[j]]=1;
max = j-i+1>max?j-i+1:max;
}
}
s++;
}
free(cnt);
return max;
}
int main()
{
char *s = malloc(256);
int ans;
while(1)
{
scanf("%s",s);
ans = lengthOfLongestSubstring(s);
printf("%d",ans);
}
}