-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1071.go
More file actions
43 lines (39 loc) · 856 Bytes
/
Copy path1071.go
File metadata and controls
43 lines (39 loc) · 856 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"strings"
"fmt"
)
func gcdRecursive(str1 string, str2 string, prev string) string{
if len(str1) < len(str2) {
return gcdRecursive(str2, str1, prev)
}
i, j := 0,0
for ; i < len(str1) ; {
if str1[i] == str2[j] {
i++
j++
if j >= len(str2) {
j = 0
}
} else {
return ""
}
}
gcd := str2[j:]
// fmt.Println("GCD=",gcd)
// fmt.Println("prev=",prev)
if len(gcd) == 0 || strings.Compare(prev, gcd) == 0 {
return prev
}
prev = gcd
return gcdRecursive(str2, gcd, prev)
}
func gcdOfStrings(str1 string, str2 string) string {
return gcdRecursive(str1, str2, "")
}
func main(){
str1 := "ABABABAB"
str2 := "ABAB"
ret := gcdOfStrings(str1, str2)
fmt.Println(ret)
}