FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/String/LevenshteinDistance.js at master · TheAlgorithms/JavaScript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
TheAlgorithms
/
JavaScript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
5.8k
Star
34.2k
Code
Issues
21
Pull requests
192
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript
/
String
/
LevenshteinDistance.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (37 loc) · 1.32 KB
Breadcrumbs
JavaScript
/
String
/
LevenshteinDistance.js
Copy path
File metadata and controls
43 lines (37 loc) · 1.32 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/* The Levenshtein distance (a.k.a edit distance) is a
measure of similarity between two strings. It is
defined as the minimum number of changes required to
convert string a into string b (this is done by
inserting, deleting or replacing a character in
string a).
The smaller the Levenshtein distance,
the more similar the strings are. This is a very
common problem in the application of Dynamic Programming.
*/
const
levenshteinDistance
=
(
a
,
b
)
=>
{
// Declaring array 'D' with rows = len(a) + 1 and columns = len(b) + 1:
const
distanceMatrix
=
Array
(
b
.
length
+
1
)
.
fill
(
null
)
.
map
(
(
)
=>
Array
(
a
.
length
+
1
)
.
fill
(
null
)
)
// Initializing first column:
for
(
let
i
=
0
;
i
<=
a
.
length
;
i
+=
1
)
{
distanceMatrix
[
0
]
[
i
]
=
i
}
// Initializing first row:
for
(
let
j
=
0
;
j
<=
b
.
length
;
j
+=
1
)
{
distanceMatrix
[
j
]
[
0
]
=
j
}
for
(
let
j
=
1
;
j
<=
b
.
length
;
j
+=
1
)
{
for
(
let
i
=
1
;
i
<=
a
.
length
;
i
+=
1
)
{
const
indicator
=
a
[
i
-
1
]
===
b
[
j
-
1
]
?
0
:
1
// choosing the minimum of all three, vis-a-vis:
distanceMatrix
[
j
]
[
i
]
=
Math
.
min
(
distanceMatrix
[
j
]
[
i
-
1
]
+
1
,
// deletion
distanceMatrix
[
j
-
1
]
[
i
]
+
1
,
// insertion
distanceMatrix
[
j
-
1
]
[
i
-
1
]
+
indicator
// substitution
)
}
}
return
distanceMatrix
[
b
.
length
]
[
a
.
length
]
}
export
{
levenshteinDistance
}
Back
|
FazBrowse Home
|
New Git URL