Skip to content

Commit 8cd5fb9

Browse files
committed
[Create] LeetCode_1669 연속 반복 문자열
1 parent 42635ad commit 8cd5fb9

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

RepeatSubstring.java

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// LeetCode_1669
2+
// 2021.04.22
3+
// Easy
4+
5+
import java.util.ArrayList;
6+
import java.util.Comparator;
7+
import java.util.List;
8+
9+
public class RepeatSubstring {
10+
public static void main(String[] args) {
11+
System.out.println(new RepeatSubstring().maxRepeating("ababc","ab"));
12+
}
13+
14+
public int maxRepeating(String sequence, String word) {
15+
List<Integer> list = new ArrayList<>();
16+
String subSequence = sequence;
17+
StringBuilder str;
18+
int cnt = 0;
19+
int preIdx = word.length();
20+
int tmp = 0;
21+
list.add(0);
22+
23+
while(subSequence.contains(word)) {
24+
str = checkSubstring(subSequence, word);
25+
subSequence = str.toString();
26+
if(preIdx == sequence.indexOf(word)+word.length()) {
27+
cnt++;
28+
tmp = cnt;
29+
}
30+
else{
31+
list.add(cnt);
32+
cnt = 1;
33+
}
34+
35+
sequence = subSequence;
36+
}
37+
if(list.size() == 1)
38+
list.add(tmp);
39+
list.sort(Comparator.reverseOrder());
40+
return list.get(0);
41+
}
42+
43+
public static StringBuilder checkSubstring(String str, String word){
44+
StringBuilder s = new StringBuilder(str);
45+
String subString ="";
46+
47+
if(s.indexOf(word) != -1){
48+
subString = s.substring(s.indexOf(word) + word.length(), s.length());
49+
}
50+
51+
return new StringBuilder(subString);
52+
}
53+
}
54+
55+
/* simple Solution..출처: LeetCode
56+
public int maxRepeating(String sequence, String word) {
57+
58+
59+
String w1 = word;
60+
int res = 0;
61+
while (w1.length() <= sequence.length()) {
62+
if (sequence.contains(w1)) {
63+
res++;
64+
w1 += word;
65+
} else {
66+
break;
67+
}
68+
}
69+
70+
return res;
71+
}
72+
*/

0 commit comments

Comments
 (0)