Comment on page

# 1062.Longest-Repeating-Substring

## 题目描述

Given a string S, find out the length of the longest repeating substring(s). Return 0 if no repeating substring exists.
Example 1:
Input: "abcd"
Output: 0
Explanation: There is no repeating substring.
Example 2:
Input: "abbaba"
Output: 2
Explanation: The longest repeating substrings are "ab" and "ba", each of which occurs twice.
Example 3:
Input: "aabcaabdaab"
Output: 3
Explanation: The longest repeating substring is "aab", which occurs 3 times.
Example 4:
Input: "aaaaa"
Output: 4
Explanation: The longest repeating substring is "aaaa", which occurs twice.
Note:
The string S consists of only lowercase English letters from 'a' - 'z'.
1 <= S.length <= 1500

## 代码

### Approach #1 Binary Search + HashSet

class Solution {
public int longestRepeatingSubstring(String S) {
int n = S.length();
int left = 1;
int right = n;
int L;
while (left <= right) {
L = left + (right - left) / 2;
if (search(L, n, S) == -1) {
right = L - 1;
} else {
left = L + 1;
}
}
return left - 1;
}
private int search(int L, int n, String S) {
HashSet<String> seen = new HashSet();
String tmp;
for (int start = 0; start < n - L + 1; start++) {
tmp = S.substring(start, start + L);
if (seen.contains(tmp)) return start;