-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
72 lines (69 loc) · 2.09 KB
/
Program.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
using System;
using System.Runtime.InteropServices.WindowsRuntime;
namespace OneEditDistance
{
class Program
{
static void Main(string[] args)
{
string s = "teacher", t = "detacher";
Console.WriteLine(IsOneEditDistance(s, t));
}
static bool IsOneEditDistance(string s, string t)
{
if (Math.Abs(s.Length - t.Length) > 1) return false;
if (s.Length == t.Length)
{
if (s == t) return false;
int count = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] != t[i])
count++;
if (count > 1)
return false;
}
return count == 1;
}
if (s.Length > t.Length)
return ValidateString(s, t);
return ValidateString(t, s);
}
static bool ValidateString(string s, string t)
{
for (int i = 0; i < s.Length; i++)
{
string temp = s.Substring(0, i - 0);
temp += i == s.Length - 1 ? "" : s.Substring(i + 1, s.Length - i - 1);
if (t == temp)
return true;
}
return false;
}
static bool IsOneEditDistance_2(string s, string t)
{
if (s.Length <= t.Length)
return s != t && Check(s, t);
return Check(t, s);
}
static bool Check(string s, string t)
{
if (Math.Abs(s.Length - t.Length) > 1) return false;
if (s == "" || t == "") return true;
int p1 = 0, p2 = 0, count = 0;
while (p1 < s.Length && p2 < t.Length)
{
if (s[p1] != t[p2])
{
count++;
if (s.Length != t.Length)
p1--;
}
if (count > 1) return false;
p1++;
p2++;
}
return count <= 1;
}
}
}