From 032ac3b9aede7027a286ee52857d686e48b339a8 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 7 Oct 2024 20:40:38 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB1813.=20Sentence=20Similarity?= =?UTF-8?q?=20III=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- go/sentence-similarity-iii.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 go/sentence-similarity-iii.go diff --git a/go/sentence-similarity-iii.go b/go/sentence-similarity-iii.go new file mode 100644 index 0000000..18a5651 --- /dev/null +++ b/go/sentence-similarity-iii.go @@ -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) +}