-
Notifications
You must be signed in to change notification settings - Fork 6
/
KPM.java
45 lines (39 loc) · 982 Bytes
/
KPM.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
35
36
37
38
39
40
41
42
43
44
45
import java.util.ArrayList;
import java.util.List;
public class KPM
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
String str = "AbhishekisthbestboytobeAbhishekisgoingto be amazing good but nkoiAbuishek of it but once again gringotypeAbhishekisthbestbostTo";
char[] charArray = str.toCharArray();
int pointer = 0;
List<String> list = new ArrayList<>();
List<String> dummy = new ArrayList<>();
if (charArray.length > 1)
{
for (int i = 1; i < charArray.length; i++)
{
if(charArray[i] == charArray[pointer]){
dummy.add(""+charArray[i]);
pointer++;
}else{
pointer = 0;
if(list.size() < dummy.size()){
list.clear();
list.addAll(dummy);
}
dummy.clear();
}
}
}
else
{
System.err.println(str);
}
if(dummy.size() > list.size()){
list.clear();list.addAll(dummy);
}
System.out.println(list);
}
}