go: add «1813. Sentence Similarity III»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
a8567738b8
commit
032ac3b9ae
1 changed files with 29 additions and 0 deletions
29
go/sentence-similarity-iii.go
Normal file
29
go/sentence-similarity-iii.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package main
|
||||
|
||||
import "strings"
|
||||
|
||||
func areSentencesSimilar(sentence1 string, sentence2 string) bool {
|
||||
checkSimilar := func(words1, words2 []string) bool {
|
||||
left := 0
|
||||
for left < len(words1) && words1[left] == words2[left] {
|
||||
left++
|
||||
}
|
||||
|
||||
right1, right2 := len(words1)-1, len(words2)-1
|
||||
for right1 >= 0 && words1[right1] == words2[right2] {
|
||||
right1--
|
||||
right2--
|
||||
}
|
||||
|
||||
return right1 < left
|
||||
}
|
||||
|
||||
words1 := strings.Split(sentence1, " ")
|
||||
words2 := strings.Split(sentence2, " ")
|
||||
|
||||
if len(words1) > len(words2) {
|
||||
words1, words2 = words2, words1
|
||||
}
|
||||
|
||||
return checkSimilar(words1, words2)
|
||||
}
|
Loading…
Reference in a new issue