Skip to content

Instantly share code, notes, and snippets.

@fernandoporazzi
Created January 10, 2019 10:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fernandoporazzi/1382b156bb1743adaa268a22a4039872 to your computer and use it in GitHub Desktop.
Save fernandoporazzi/1382b156bb1743adaa268a22a4039872 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"strings"
)
func CompareTwoStrings(stringOne, stringTwo string) float32 {
removeSpaces(&stringOne, &stringTwo)
if value := returnEarlyIfPossible(stringOne, stringTwo); value >= 0 {
return value
}
firstBigrams := make(map[string]int)
for i := 0; i < len(stringOne)-1; i++ {
a := fmt.Sprintf("%c", stringOne[i])
b := fmt.Sprintf("%c", stringOne[i+1])
bigram := a + b
var count int
if value, ok := firstBigrams[bigram]; ok {
count = value + 1
} else {
count = 1
}
firstBigrams[bigram] = count
}
var intersectionSize float32
intersectionSize = 0
for i := 0; i < len(stringTwo)-1; i++ {
a := fmt.Sprintf("%c", stringTwo[i])
b := fmt.Sprintf("%c", stringTwo[i+1])
bigram := a + b
var count int
if value, ok := firstBigrams[bigram]; ok {
count = value
} else {
count = 0
}
if count > 0 {
firstBigrams[bigram] = count - 1
intersectionSize = intersectionSize + 1
}
}
return (2.0 * intersectionSize) / (float32(len(stringOne)) + float32(len(stringTwo)) - 2)
}
func main() {
fmt.Println(CompareTwoStrings("drinking coffee makes me happy", "drinking tea makes me happy"))
}
func removeSpaces(stringOne, stringTwo *string) {
*stringOne = strings.Replace(*stringOne, " ", "", -1)
*stringTwo = strings.Replace(*stringTwo, " ", "", -1)
}
func returnEarlyIfPossible(stringOne, stringTwo string) float32 {
// if both are empty strings
if len(stringOne) == 0 && len(stringTwo) == 0 {
return 1
}
// if only one is empty string
if len(stringOne) == 0 || len(stringTwo) == 0 {
return 0
}
// identical
if stringOne == stringTwo {
return 1
}
// both are 1-letter strings
if len(stringOne) == 1 && len(stringTwo) == 1 {
return 0
}
// if either is a 1-letter string
if len(stringOne) < 2 || len(stringTwo) < 2 {
return 0
}
return -1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment