-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecurring_string.c
79 lines (69 loc) · 1.19 KB
/
recurring_string.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
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*brute force O(N2) - error */
char find_recurring_char(char *);
/*O(N) */
char make_map(char *);
int main () {
char string[100];
printf("Enter the string \n");
scanf("%s",string);
printf("recurring char is %c\n",find_recurring_char(string));
printf("recurring char is %c \n",make_map(string));
}
char find_recurring_char (char * c) {
int index=0;
int index1=0;
int length=0;
char rec=0;
int distance=0;
while(c[index]!=0) {
index++;
}
length=index;
index=0;
index1=1;
while (index <length) {
if (c[index]==c[index1]) {
if (index==0) {
distance=index1-index;
rec=c[index];
} else {
if ((index1-index)<distance) {
distance=index1-index;
rec=c[index];
}
}
//return c[index];
} else {
if(index1<length) {
index1++;
continue;
}
}
index++;
index1=index+1;
}
return rec;
}
char make_map(char *c) {
int index=0;
int index1=0;
int length=0;
char map[255];
while(c[index]!=0) {
index++;
}
length=index;
index=0;
memset(map,0,255);
while (index <length) {
map[c[index]]+=1;
if (map[c[index]]>1) {
return c[index];
}
index++;
}
return 0;
}