邓心一 před 6 roky
rodič
revize
6f0412932e
1 změnil soubory, kde provedl 13 přidání a 0 odebrání
  1. 13 0
      easy/686.repeated-string-match.go

+ 13 - 0
easy/686.repeated-string-match.go

@@ -0,0 +1,13 @@
+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
+}