go: add «1071. Greatest Common Divisor of Strings»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-08-12 17:40:55 +02:00
parent e9e304b2aa
commit e5b9d8aa1c
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -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]
}