-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1062-LongestRepeatingSubstring.go
More file actions
64 lines (56 loc) · 1.8 KB
/
1062-LongestRepeatingSubstring.go
File metadata and controls
64 lines (56 loc) · 1.8 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
package main
// 1062. Longest Repeating Substring
// Given a string s, return the length of the longest repeating substrings.
// If no repeating substring exists, return 0.
// Example 1:
// Input: s = "abcd"
// Output: 0
// Explanation: There is no repeating substring.
// Example 2:
// Input: s = "abbaba"
// Output: 2
// Explanation: The longest repeating substrings are "ab" and "ba", each of which occurs twice.
// Example 3:
// Input: s = "aabcaabdaab"
// Output: 3
// Explanation: The longest repeating substring is "aab", which occurs 3 times.
// Constraints:
// 1 <= s.length <= 2000
// s consists of lowercase English letters.
import "fmt"
import "sort"
func longestRepeatingSubstring(s string) int {
res, n := 0, len(s)
suffix := make([]string, n)
for i := 1; i <= n; i++ { // 计算后缀数组
suffix[i-1] = s[n-i:]
}
sort.Strings(suffix) // 排序 (前缀相同的字符串一定相邻)
for i := 1; i < n; i++ {
j := 0
for j < len(suffix[i-1]) && j < len(suffix[i]) && suffix[i-1][j] == suffix[i][j] { // 计算相邻的字符串的最长公共前缀
j++
}
if j > res {
res = j
}
}
return res
}
func main() {
// Example 1:
// Input: s = "abcd"
// Output: 0
// Explanation: There is no repeating substring.
fmt.Println(longestRepeatingSubstring("abcd")) // 0
// Example 2:
// Input: s = "abbaba"
// Output: 2
// Explanation: The longest repeating substrings are "ab" and "ba", each of which occurs twice.
fmt.Println(longestRepeatingSubstring("abbaba")) // 2
// Example 3:
// Input: s = "aabcaabdaab"
// Output: 3
// Explanation: The longest repeating substring is "aab", which occurs 3 times.
fmt.Println(longestRepeatingSubstring("aabcaabdaab")) // 3
}