1062:最长重复子串
This commit is contained in:
parent
dbc12a884c
commit
982ea7663c
@ -0,0 +1,84 @@
|
|||||||
|
//<p>给定字符串 <code>S</code>,找出最长重复子串的长度。如果不存在重复子串就返回 <code>0</code>。</p>
|
||||||
|
//
|
||||||
|
//<p> </p>
|
||||||
|
//
|
||||||
|
//<p><strong>示例 1:</strong></p>
|
||||||
|
//
|
||||||
|
//<pre><strong>输入:</strong>"abcd"
|
||||||
|
//<strong>输出:</strong>0
|
||||||
|
//<strong>解释:</strong>没有重复子串。
|
||||||
|
//</pre>
|
||||||
|
//
|
||||||
|
//<p><strong>示例 2:</strong></p>
|
||||||
|
//
|
||||||
|
//<pre><strong>输入:</strong>"abbaba"
|
||||||
|
//<strong>输出:</strong>2
|
||||||
|
//<strong>解释:</strong>最长的重复子串为 "ab" 和 "ba",每个出现 2 次。
|
||||||
|
//</pre>
|
||||||
|
//
|
||||||
|
//<p><strong>示例 3:</strong></p>
|
||||||
|
//
|
||||||
|
//<pre><strong>输入:</strong>"aabcaabdaab"
|
||||||
|
//<strong>输出:</strong>3
|
||||||
|
//<strong>解释:</strong>最长的重复子串为 "aab",出现 3 次。
|
||||||
|
//</pre>
|
||||||
|
//
|
||||||
|
//<p><strong>示例 4:</strong></p>
|
||||||
|
//
|
||||||
|
//<pre><strong>输入:</strong>"aaaaa"
|
||||||
|
//<strong>输出:</strong>4
|
||||||
|
//<strong>解释:</strong>最长的重复子串为 "aaaa",出现 2 次。</pre>
|
||||||
|
//
|
||||||
|
//<p> </p>
|
||||||
|
//
|
||||||
|
//<p><strong>提示:</strong></p>
|
||||||
|
//
|
||||||
|
//<ol>
|
||||||
|
// <li>字符串 <code>S</code> 仅包含从 <code>'a'</code> 到 <code>'z'</code> 的小写英文字母。</li>
|
||||||
|
// <li><code>1 <= S.length <= 1500</code></li>
|
||||||
|
//</ol>
|
||||||
|
//<div><div>Related Topics</div><div><li>字符串</li><li>二分查找</li><li>动态规划</li><li>后缀数组</li><li>哈希函数</li><li>滚动哈希</li></div></div><br><div><li>👍 72</li><li>👎 0</li></div>
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.hash.Hash;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
// 1062:最长重复子串
|
||||||
|
public class LongestRepeatingSubstring {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Solution solution = new LongestRepeatingSubstring().new Solution();
|
||||||
|
// TO TEST
|
||||||
|
}
|
||||||
|
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int longestRepeatingSubstring(String s) {
|
||||||
|
int left = 1;
|
||||||
|
int right = s.length();
|
||||||
|
while (left <= right) {
|
||||||
|
int mid = (right - left) / 2 + left;
|
||||||
|
if (search(s, mid) != -1) {
|
||||||
|
left = mid + 1;
|
||||||
|
} else {
|
||||||
|
right = mid - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return left - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int search(String str, int length) {
|
||||||
|
HashSet<Integer> hashSet = new HashSet<>();
|
||||||
|
for (int i = 0; i <= str.length() - length; i++) {
|
||||||
|
int hashCode = str.substring(i, i + length).hashCode();
|
||||||
|
if (hashSet.contains(hashCode)) {
|
||||||
|
return hashCode;
|
||||||
|
}
|
||||||
|
hashSet.add(hashCode);
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
<p>给定字符串 <code>S</code>,找出最长重复子串的长度。如果不存在重复子串就返回 <code>0</code>。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>"abcd"
|
||||||
|
<strong>输出:</strong>0
|
||||||
|
<strong>解释:</strong>没有重复子串。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>"abbaba"
|
||||||
|
<strong>输出:</strong>2
|
||||||
|
<strong>解释:</strong>最长的重复子串为 "ab" 和 "ba",每个出现 2 次。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>"aabcaabdaab"
|
||||||
|
<strong>输出:</strong>3
|
||||||
|
<strong>解释:</strong>最长的重复子串为 "aab",出现 3 次。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 4:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>"aaaaa"
|
||||||
|
<strong>输出:</strong>4
|
||||||
|
<strong>解释:</strong>最长的重复子串为 "aaaa",出现 2 次。</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ol>
|
||||||
|
<li>字符串 <code>S</code> 仅包含从 <code>'a'</code> 到 <code>'z'</code> 的小写英文字母。</li>
|
||||||
|
<li><code>1 <= S.length <= 1500</code></li>
|
||||||
|
</ol>
|
||||||
|
<div><div>Related Topics</div><div><li>字符串</li><li>二分查找</li><li>动态规划</li><li>后缀数组</li><li>哈希函数</li><li>滚动哈希</li></div></div><br><div><li>👍 72</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user