Christian Sheridan hace 5 años
padre
commit
b57ef9e7dd

+ 12
- 8
src/main/java/rocks/zipcode/io/quiz4/fundamentals/StringEvaluator.java Ver fichero

1
 package rocks.zipcode.io.quiz4.fundamentals;
1
 package rocks.zipcode.io.quiz4.fundamentals;
2
 
2
 
3
+import java.util.ArrayList;
4
+
3
 /**
5
 /**
4
  * @author leon on 11/12/2018.
6
  * @author leon on 11/12/2018.
5
  */
7
  */
13
     }
15
     }
14
 
16
 
15
     public static String[] getAllPrefixes(String string) {
17
     public static String[] getAllPrefixes(String string) {
16
-        int arrayLength = factorial(string.length()-1);
17
-        String[] prefixes = new String[arrayLength];
18
+        ArrayList<String> prefixes = new ArrayList<>();
18
         if(string.length()==1){
19
         if(string.length()==1){
19
-            prefixes[0] = string;
20
+            prefixes.add(string);
21
+            String[] arr = new String[prefixes.size()];
22
+            arr = prefixes.toArray(arr);
23
+            return arr;
20
         }
24
         }
21
         for(int i =0; i<string.length(); i++){
25
         for(int i =0; i<string.length(); i++){
22
-            for(int j = 0; j<string.length(); j++){
23
-                if(i<j) {
24
-                    prefixes[i + j] = string.substring(i, j);
25
-                }
26
+            for(int j = string.length(); j>i; j--){
27
+                    prefixes.add(string.substring(i, j));
26
             }
28
             }
27
         }
29
         }
28
-        return prefixes;
30
+        String[] arr = new String[prefixes.size()];
31
+        arr = prefixes.toArray(arr);
32
+        return arr;
29
     }
33
     }
30
 
34
 
31
     public static String[] getCommonPrefixes(String string1, String string2) {
35
     public static String[] getCommonPrefixes(String string1, String string2) {