-
Notifications
You must be signed in to change notification settings - Fork 153
/
One_edit_away.c
94 lines (93 loc) · 1.86 KB
/
One_edit_away.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* @author: Rajdeep Roy Chowdhury<[email protected]>
* @github: https://github.com/razdeep
* @date: 24/12/2018
*/
#include <stdio.h>
#include <string.h>
int replacable(const char *s1, const char *s2)
{
int diff_count = 0;
int s1_len = strlen(s1);
int s2_len = strlen(s2);
if (s1_len == s2_len)
{
for (int i = 0; i < s1_len; i++)
{
if (s1[i] != s2[i])
diff_count++;
}
if (diff_count == 1)
return 1;
}
return 0;
}
int addable(const char *s1, const char *s2)
{
int diff_count = 0;
int s1_len = strlen(s1);
int s2_len = strlen(s2);
if (s1_len == s2_len + 1)
{
int j = 0;
for (int i = 0; i < s1_len; i++)
{
if (s1[i] == s2[j])
{
j++;
}
else
{
diff_count++;
}
}
if (diff_count <= 1)
return 1;
else
return 0;
}
return 0;
}
int deletable(const char *s1, const char *s2)
{
int diff_count = 0;
int s1_len = strlen(s1);
int s2_len = strlen(s2);
if (s1_len + 1 == s2_len)
{
int j = 0;
for (int i = 0; i < s2_len; i++)
{
if (s2[i] == s1[j])
{
j++;
}
else
{
diff_count++;
}
}
if (diff_count <= 1)
return 1;
else
return 0;
}
return 0;
}
int main()
{
char s1[100], s2[100];
printf("Enter the first string ");
scanf("%s", s1);
printf("Enter the second string ");
scanf("%s", s2);
if (strcmp(s1, s2) == 0 || replacable(s1, s2) || addable(s1, s2) || deletable(s1, s2))
{
printf("Yes\n");
}
else
{
printf("No\n");
}
return 0;
}