Skip to content

Commit

Permalink
GCD of 2 strings in golang
Browse files Browse the repository at this point in the history
  • Loading branch information
absognety committed Jun 24, 2024
1 parent e487ccd commit 3566205
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
17 changes: 17 additions & 0 deletions LeetCode/ProgrammingInGo/gcd/gcdofstrings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gcd

func gcd_util(a int, b int) int {
for b != 0 {
t := b
b = a % b
a = t
}
return a
}
func GCDofStrings(str1 string, str2 string) string {
null_str := ""
if (str1 + str2) != (str2 + str1) {
return null_str
}
return str1[0:gcd_util(len(str1), len(str2))]
}
6 changes: 6 additions & 0 deletions LeetCode/ProgrammingInGo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"ProgrammingInGo/MinMovesToSeatEveryone"
"ProgrammingInGo/MergeAlternately"
"ProgrammingInGo/gcd"
)

func main() {
Expand All @@ -17,4 +18,9 @@ func main() {
fmt.Println(MergeAlternately.MergeAlternate("abc", "pqr"))
fmt.Println(MergeAlternately.MergeAlternate("ab", "pqrs"))
fmt.Println(MergeAlternately.MergeAlternate("abcd", "pq"))

// test the function
fmt.Println(gcd.GCDofStrings("ABCABC", "ABC"))
fmt.Println(gcd.GCDofStrings("ABABAB", "ABAB"))
fmt.Println(gcd.GCDofStrings("LEET", "CODE"))
}

0 comments on commit 3566205

Please sign in to comment.