-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathLevenshtein.cs
31 lines (28 loc) · 968 Bytes
/
Levenshtein.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
using UnityEngine;
using System;
public class Levenshtein : MonoBehaviour
{
int Distance(string a, string b)
{
int[,] m = new int[a.Length + 1, b.Length + 1];
for (int i = 0; i <= a.Length; i++) m[i, 0] = i;
for (int j = 0; j <= b.Length; j++) m[0, j] = j;
for (int i = 1; i <= a.Length; i++)
{
for (int j = 1; j <= b.Length; j++)
{
int cost = (a[i - 1] == b[j - 1]) ? 0 : 1;
m[i, j] = Math.Min(Math.Min(m[i - 1, j] + 1, m[i, j - 1] + 1), m[i - 1, j - 1] + cost);
}
}
return m[a.Length, b.Length];
}
void Start()
{
Debug.Log(Distance("word", "swords")); // 2
Debug.Log(Distance("brake", "break")); // 2
}
}
/* Levenshtein distance is a string metric for measuring the difference between two sequences.
Informally, the Levenshtein distance between two words is the minimum number of single-character edits (insertions, deletions or substitutions)
required to change one word into the other. */