-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubStringEqual0and1.c
47 lines (43 loc) · 1.07 KB
/
subStringEqual0and1.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
// Given a string , find substring(max length) containing equal number of 0 and 1
// This question is not done!!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void findSubString(char*string){
int length=strlen(string);
//stores cummulative
int*storage=(int*)calloc(length+1,sizeof(int));
//lookup array
int*counter=(int*)calloc(100000,sizeof(int));
for(int i=0;i<100000;i++){
counter[i]=-1;
}
int max=0;
storage[0]=length;
for(int i=1;i<length+1;i++){
if(string[i-1]=='0'){
storage[i]=storage[i-1]+(-1);
}
else if(string[i-1]=='1'){
storage[i]=storage[i-1]+1;
}
}
for(int i=0;i<length+1;i++){
if(counter[storage[i]]==-1){
counter[storage[i]]=i;
}
else{
if(i-counter[storage[i]]>max){
max=i-counter[storage[i]];
}
}
}
printf("%d",max);
}
int main(){
char*input=(char*)calloc(100000,sizeof(char));
scanf("%s",input);
findSubString(input);
free(input);
return 0;
}