-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
656e0f2
commit 910d5d7
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package acmd | ||
|
||
// strDistance between 2 strings using Levenshtein distance algorithm. | ||
func strDistance(a, b string) int { | ||
switch { | ||
case a == "": | ||
return len(b) | ||
case b == "": | ||
return len(a) | ||
case a == b: | ||
return 0 | ||
} | ||
|
||
if len(a) > len(b) { | ||
a, b = b, a | ||
} | ||
lenA, lenB := len(a), len(b) | ||
|
||
x := make([]int, lenA+1) | ||
for i := 0; i < len(x); i++ { | ||
x[i] = i | ||
} | ||
|
||
for i := 1; i <= lenB; i++ { | ||
prev := i | ||
for j := 1; j <= lenA; j++ { | ||
current := x[j-1] // match | ||
if b[i-1] != a[j-1] { | ||
current = min3(x[j-1]+1, prev+1, x[j]+1) | ||
} | ||
x[j-1], prev = prev, current | ||
} | ||
x[lenA] = prev | ||
} | ||
return x[lenA] | ||
} | ||
|
||
func min3(a, b, c int) int { | ||
if a < b { | ||
if a < c { | ||
return a | ||
} | ||
} | ||
if b < c { | ||
return b | ||
} | ||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package acmd | ||
|
||
import "testing" | ||
|
||
func Test_strDistance(t *testing.T) { | ||
testCases := []struct { | ||
a, b string | ||
want int | ||
}{ | ||
{"", "", 0}, | ||
{"a", "a", 0}, | ||
{"", "hello", 5}, | ||
{"hello", "", 5}, | ||
{"hello", "hello", 0}, | ||
{"ab", "aa", 1}, | ||
{"ab", "ba", 2}, | ||
{"ab", "aaa", 2}, | ||
{"bbb", "a", 3}, | ||
{"kitten", "sitting", 3}, | ||
{"distance", "difference", 5}, | ||
{"resume and cafe", "resumes and cafes", 2}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
dist := strDistance(tc.a, tc.b) | ||
if dist != tc.want { | ||
t.Errorf("for (%q , %q) want %d, got %d", tc.a, tc.b, tc.want, dist) | ||
} | ||
} | ||
} |