go: add «1071. Greatest Common Divisor of Strings»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
e9e304b2aa
commit
e5b9d8aa1c
1 changed files with 17 additions and 0 deletions
17
go/greatest-common-divisor-of-strings.go
Normal file
17
go/greatest-common-divisor-of-strings.go
Normal 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]
|
||||
}
|
Loading…
Reference in a new issue