package main
import "fmt"
func main () {
str1 := "ABABAB"
str2 := "AB"
fmt.Println("input:")
fmt.Println("", str1)
fmt.Println("", str2)
gcd := gcdOfStrings("ABABAB", "AB")
fmt.Println("output:")
fmt.Println("", gcd)
}
func gcdOfStrings(str1 string, str2 string) string {
if len(str1) < len(str2) {
str1, str2 = str2, str1
}
var remainder = modOfStrings(str1, str2)
if remainder == "" {
return str2
} else if remainder == str1 {
return ""
}
return gcdOfStrings(str2, remainder)
}
func modOfStrings(str1, str2 string) string {
length := len(str2)
var remainder = str1
for {
if len(remainder) < length || remainder[:length] != str2 {
break
}
remainder = remainder[length:]
}
return remainder
}