|
@@ -1,18 +1,53 @@
|
1
|
1
|
package rocks.zipcode.io.quiz4.fundamentals;
|
2
|
2
|
|
|
3
|
+import java.util.ArrayList;
|
|
4
|
+import java.util.LinkedHashSet;
|
|
5
|
+import java.util.List;
|
|
6
|
+import org.apache.commons.lang3.StringUtils;
|
|
7
|
+
|
3
|
8
|
/**
|
4
|
9
|
* @author leon on 11/12/2018.
|
5
|
10
|
*/
|
6
|
11
|
public class StringEvaluator {
|
7
|
12
|
public static String[] getAllPrefixes(String string) {
|
8
|
|
- return null;
|
|
13
|
+ List<String> pre = new ArrayList<>();
|
|
14
|
+ for (int i = 0; i <= string.length(); i++){
|
|
15
|
+ for (int j = i; j < string.length(); j++){
|
|
16
|
+ pre.add(string.substring(i, j+1));
|
|
17
|
+ }
|
|
18
|
+ }
|
|
19
|
+ pre = new ArrayList<String>(new LinkedHashSet<String>(pre));
|
|
20
|
+ return pre.toArray(new String[0]);
|
9
|
21
|
}
|
10
|
22
|
|
11
|
23
|
public static String[] getCommonPrefixes(String string1, String string2) {
|
12
|
|
- return null;
|
|
24
|
+ String prefix = "";
|
|
25
|
+ int min = Math.min(string1.length(), string2.length());
|
|
26
|
+ for (int i = 0; i < min; i++){
|
|
27
|
+ if (string1.charAt(i) == string2.charAt(i)){
|
|
28
|
+ prefix = string1.substring(0, i);
|
|
29
|
+ }
|
|
30
|
+ }
|
|
31
|
+ return new String[]{prefix};
|
13
|
32
|
}
|
14
|
33
|
|
15
|
34
|
public static String getLargestCommonPrefix(String string1, String string2) {
|
16
|
|
- return null;
|
|
35
|
+ int start = 0;
|
|
36
|
+ int max = 0;
|
|
37
|
+ for (int i = 0; i < string1.length(); i++){
|
|
38
|
+ for (int j = 0; j < string2.length(); j++){
|
|
39
|
+ int x = 0;
|
|
40
|
+ while (string1.charAt(i + x) == string2.charAt(j + x)){
|
|
41
|
+ x++;
|
|
42
|
+ if (((i + x) >= string1.length()) || ((j + x) >= string2.length()))
|
|
43
|
+ break;
|
|
44
|
+ }
|
|
45
|
+ if (x > max){
|
|
46
|
+ max = x;
|
|
47
|
+ start = i;
|
|
48
|
+ }
|
|
49
|
+ }
|
|
50
|
+ }
|
|
51
|
+ return string1.substring(start, (start + max));
|
17
|
52
|
}
|
18
|
53
|
}
|