-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path459-RepeatedSubstringPattern.go
More file actions
83 lines (71 loc) · 2.22 KB
/
459-RepeatedSubstringPattern.go
File metadata and controls
83 lines (71 loc) · 2.22 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
// 459. Repeated Substring Pattern
// Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
// Example 1:
// Input: s = "abab"
// Output: true
// Explanation: It is the substring "ab" twice.
// Example 2:
// Input: s = "aba"
// Output: false
// Example 3:
// Input: s = "abcabcabcabc"
// Output: true
// Explanation: It is the substring "abc" four times or the substring "abcabc" twice.
// Constraints:
// 1 <= s.length <= 10^4
// s consists of lowercase English letters.
import "fmt"
import "strings"
func repeatedSubstringPattern(s string) bool {
l := len(s)
for i := 1; i <= l/2; i++ {
if l%i == 0 {
substring := s[:i]
var builder strings.Builder
for j := 0; j < l/i; j++ {
builder.WriteString(substring)
}
if builder.String() == s {
return true
}
}
}
return false
}
// best solution
func repeatedSubstringPattern1(s string) bool {
n := len(s)
next := make([]int, n)
for i := range next {
next[i] = -1
}
j := -1
for i := 1; i < n; i ++ {
for j >= 0 && s[i] != s[j+1] {
j = next[j]
}
if s[i] == s[j+1] {
j++
}
next[i] = j
}
if next[n-1] != -1 && n % (n - 1 - next[n-1]) == 0 {
return true
}
return false
}
func main() {
// Explanation: It is the substring "ab" twice.
fmt.Println(repeatedSubstringPattern("abab")) // true
fmt.Println(repeatedSubstringPattern("aba")) // false
// Explanation: It is the substring "abc" four times or the substring "abcabc" twice.
fmt.Println(repeatedSubstringPattern("abcabcabcabc")) // true
fmt.Println(repeatedSubstringPattern("abcabcabc")) // true
// Explanation: It is the substring "ab" twice.
fmt.Println(repeatedSubstringPattern1("abab")) // true
fmt.Println(repeatedSubstringPattern1("aba")) // false
// Explanation: It is the substring "abc" four times or the substring "abcabc" twice.
fmt.Println(repeatedSubstringPattern1("abcabcabcabc")) // true
fmt.Println(repeatedSubstringPattern1("abcabcabc")) // true
}