Wednesday, December 28, 2022
A function, given a string s
, to check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
import "strings"
func repeatedSubstringPattern(s string) bool {
length := len(s)
for i := length / 2; i >= 1; i-- {
// length must always be dividable by the length of repeated string
if length % i != 0 {
continue
}
// create a string and compare with given
if strings.Repeat(s[:i], length / i) == s {
return true
}
};
return false
}