12345678910111213 |
- func repeatedStringMatch(A string, B string) int {
- m, n, l := len(A), len(B), len(A)
- var sb strings.Builder
- for cnt := 1; ; cnt, l = cnt+1, l+m {
- sb.WriteString(A)
- if strings.Index(sb.String(), B) != -1 {
- return cnt
- } else if n + m < l { // If l > n + m, then each repeat of A is the same for B.
- break
- }
- }
- return -1
- }
|