-
Notifications
You must be signed in to change notification settings - Fork 1
/
MaximumRepeatingSubstring.java
34 lines (29 loc) · 1.11 KB
/
MaximumRepeatingSubstring.java
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
package com.smlnskgmail.jaman.leetcodejava.easy;
// https://leetcode.com/problems/maximum-repeating-substring/
public class MaximumRepeatingSubstring {
private final String sequence;
private final String word;
public MaximumRepeatingSubstring(String sequence, String word) {
this.sequence = sequence;
this.word = word;
}
public int solution() {
int result = 0;
for (int i = 0; i <= sequence.length() - word.length(); i++) {
if (sequence.charAt(i) == word.charAt(0)) {
String substring = sequence.substring(i, i + word.length());
int counter = 0;
if (substring.equals(word)) {
counter++;
int tempPointer = i + word.length();
while (tempPointer + word.length() <= sequence.length() && sequence.startsWith(word, tempPointer)) {
tempPointer += word.length();
counter++;
}
}
result = Math.max(counter, result);
}
}
return result;
}
}