-
Notifications
You must be signed in to change notification settings - Fork 0
/
isEditDistanceOne.cs
78 lines (71 loc) · 1.5 KB
/
isEditDistanceOne.cs
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
/*
Let the input strings be s1 and s2 and lengths of input
strings be m and n respectively.
1) If difference between m an n is more than 1,
return false.
2) Initialize count of edits as 0.
3) Start traversing both strings from first character.
a) If current characters don't match, then
(i) Increment count of edits
(ii) If count becomes more than 1, return false
(iii) If length of one string is more, then only
possible edit is to remove a character.
Therefore, move ahead in larger string.
(iv) If length is same, then only possible edit
is to change a character. Therefore, move
ahead in both strings.
b) Else, move ahead in both strings.
*/
using System;
public class Program
{
public static void Main()
{
String s1 = "gfg";
String s2 = "gf";
if (isEditDistanceOne(s1, s2))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
public static bool isEditDistanceOne(string s1, string s2)
{
int m = s1.Length;
int n = s2.Length;
if (Math.Abs(m - n) > 1)
return false;
int countedits = 0;
int i = 0;
int j = 0;
while (i < m && j < n)
{
if (s1[i] != s2[j])
{
if (m > n)
{
i++;
}
else if (m < n)
{
j++;
}
else
{
i++;
j++;
}
countedits++;
}
else
{
i++;
j++;
}
}
// If last character is extra
// in any string
if (i < m || j < n)
countedits++;
return countedits == 1;
}
}