From e5b9d8aa1cec364df61c0e4522359008dce60c6e Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 12 Aug 2024 17:40:55 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB1071.=20Greatest=20Common=20D?= =?UTF-8?q?ivisor=20of=20Strings=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- go/greatest-common-divisor-of-strings.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 go/greatest-common-divisor-of-strings.go diff --git a/go/greatest-common-divisor-of-strings.go b/go/greatest-common-divisor-of-strings.go new file mode 100644 index 0000000..f9c5b23 --- /dev/null +++ b/go/greatest-common-divisor-of-strings.go @@ -0,0 +1,17 @@ +package main + +func gcdOfStrings(str1 string, str2 string) string { + gcd := func(x, y int) int { + for y != 0 { + x, y = y, x%y + } + return x + } + + if str1+str2 != str2+str1 { + return "" + } + + length := gcd(len(str1), len(str2)) + return str1[:length] +}