Maximum Repeating Substring
给两个字符串, 问后一个重复k次后, 是不是前一个的substring.
这个题除了暴力好像没什么好办法.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Solution { public int maxRepeating(String sequence, String word) { int res = 0; int m = word.length(); int n = sequence.length(); Map<String, Integer> map = new HashMap<>(); if(m > n) return 0; for(int i = 1; i <= n / m; i++) { StringBuilder sb = new StringBuilder(); for(int j = 0; j < i; j++){ sb.append(word); } int p = sequence.indexOf(sb.toString()); if(p != -1) res = Math.max(res, i); sb.setLength(0); } return res; } } |