Skip to content

Rm/add word distance #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions dp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ Given an array representing the amount of wealth inside houses like `{1,2,3,4}`
### Minimum Deletion to Make a Palindrome

Given a string like `abccb` return the minimum number of character deletions that can be done on the string to make it a palindrome like `1` (by removing `a`, we will have `bccb`). [Solution](minimum_deletion_to_make_palindrome.go), [Tests](minimum_deletion_to_make_palindrome_test.go)

### Word Distance

Given a string like `abc`, and another string like `abcde` return how many character modifications (insert, delete, edit) have to be done on the first string to become identical to the second string. [Solution](word_distance.go.go), [Tests](word_distance.go_test.go)
2 changes: 2 additions & 0 deletions dp/minimum_deletion_to_make_palindrome.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dp

// MinimumDeletionsToMakePalindrome returns how many deletions can be done in the input string
// to make it a palindrome.
func MinimumDeletionsToMakePalindrome(input string) int {
if len(input) <= 1 {
return 0
Expand Down
45 changes: 45 additions & 0 deletions dp/word_distance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package dp

// WordDistance returns how many character modifications (insert, delete, edit) can
// be done on the first input string so that it becomes equal to the second string.
func WordDistance(input1, input2 string) int {
dp := createDP(input2, input1)
for i := 1; i <= len(input1); i++ {
for j := 1; j <= len(input2); j++ {
if input1[i-1] == input2[j-1] {
dp[i][j] = dp[i-1][j-1]
} else {
dp[i][j] = 1 + min(
dp[i-1][j], // delete
min(dp[i][j-1], // insert
dp[i-1][j-1])) // edit
}
}
}
return dp[len(input1)][len(input2)]
}

func createDP(input2 string, input1 string) [][]int {
dp := make([][]int, len(input1)+1)

for i := range dp {
dp[i] = make([]int, len(input2)+1)
}

for i := 0; i <= len(input1); i++ {
dp[i][0] = i
}

for j := 0; j <= len(input2); j++ {
dp[0][j] = j
}

return dp
}

func min(a, b int) int {
if a < b {
return a
}
return b
}
29 changes: 29 additions & 0 deletions dp/word_distance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dp

import (
"testing"
)

func TestWordDistance(t *testing.T) {
tests := []struct {
input1 string
input2 string
distance int
}{
{"", "", 0},
{"", "abcde", 5},
{"a", "a", 0},
{"a", "b", 1},
{"ab", "ac", 1},
{"ab", "dc", 2},
{"ab", "dcd", 3},
{"abcdef", "abcde", 1},
{"gabcdef", "abcde", 2},
}

for i, test := range tests {
if got := WordDistance(test.input1, test.input2); got != test.distance {
t.Fatalf("Failed test case #%d. Want %d got %d", i, test.distance, got)
}
}
}