-
Notifications
You must be signed in to change notification settings - Fork 31
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
Showing
3 changed files
with
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package day153 | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// SmallestDistanceBetweenWords returns the smallest number of words | ||
// between the two given words in the text provided. | ||
// Returns -1 if one or both of the words don't exist in the text. | ||
func SmallestDistanceBetweenWords(text, one, two string) int { | ||
words := strings.Split(text, " ") | ||
oneI := getIndex(words, one, 0) | ||
twoI := getIndex(words, two, 0) | ||
if oneI == -1 || twoI == -1 { | ||
return -1 | ||
} | ||
smallest := len(words) | ||
for oneI != -1 && twoI != -1 { | ||
if dist := abs(oneI - twoI); dist < smallest { | ||
smallest = dist | ||
} | ||
if oneI < twoI { | ||
oneI = getIndex(words, one, oneI+1) | ||
} else { | ||
twoI = getIndex(words, two, twoI+1) | ||
} | ||
} | ||
return smallest - 1 | ||
} | ||
|
||
func getIndex(s []string, target string, start int) int { | ||
for i := start; i < len(s); i++ { | ||
if s[i] == target { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
func abs(a int) int { | ||
if a < 0 { | ||
return -a | ||
} | ||
return a | ||
} |
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,29 @@ | ||
package day153 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
text, one, two string | ||
expected int | ||
}{ | ||
{"dog cat hello cat dog dog hello cat world", "hello", "world", 1}, | ||
{"dog cat hello cat dog dog hello cat world", "hello", "cat", 0}, | ||
{"dog cat hello cat dog dog hello cat world", "wat?", "cat", -1}, | ||
} | ||
|
||
func TestSmallestDistanceBetweenWords(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := SmallestDistanceBetweenWords(tc.text, tc.one, tc.two); result != tc.expected { | ||
t.Errorf("Expected %v got %v", tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkSmallestDistanceBetweenWords(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
SmallestDistanceBetweenWords(tc.text, tc.one, tc.two) | ||
} | ||
} | ||
} |