瀏覽代碼

fundamentals 100%

jasamuels 6 年之前
父節點
當前提交
2ceed2126c

+ 19
- 35
src/main/java/rocks/zipcode/io/quiz4/fundamentals/PalindromeEvaluator.java 查看文件

@@ -1,10 +1,6 @@
1 1
 package rocks.zipcode.io.quiz4.fundamentals;
2
-
3
-import java.util.HashSet;
4
-import java.util.Set;
5 2
 import java.util.SortedSet;
6 3
 import java.util.TreeSet;
7
-import java.util.stream.Stream;
8 4
 
9 5
 /**
10 6
  * @author leon on 18/12/2018.
@@ -14,42 +10,30 @@ public class PalindromeEvaluator {
14 10
     public static String[] getAllPalindromes(String string) {
15 11
 
16 12
         SortedSet<String> container = new TreeSet<>();
17
-        String tempchar;
18
-        boolean loop;
19
-
20
-        //get chars and append to a temp builder
21
-        //if chars are palindromes, add to hashset
22
-        //else move to next char
23
-        //rinse and repeat
24
-
25
-//        for (int i = 0; i < string.length() - 1; i++) {
26
-//            loop = true;
27
-//            tempchar = String.valueOf(string.charAt(i)); container.add(tempchar);
28
-//            StringBuilder temp = new StringBuilder(); temp.append(tempchar);
29
-//            StringBuilder tempreverse = new StringBuilder(); tempreverse.append(tempchar);
30
-//
31
-//            while (loop) {
32
-//                for (int j = i+1; j < string.length(); j++) {
33
-//                    temp.append(string.charAt(j));
34
-//                    tempreverse.insert(0, string.charAt(j));
35
-//                }
36
-//                if (temp.toString().equals(tempreverse.toString())) {
37
-//                    container.add(temp.toString());
38
-//                } else {
39
-//                    loop = false; }
40
-//            }
41
-//        }
42
-//        return container.stream().toArray(String[]::new);
43
-        return null;
13
+        String[] combos = StringEvaluator.getAllPrefixes(string);
14
+
15
+        for (String s: combos) {
16
+            if (string.contains(s) && isPalindrome(s)){
17
+                container.add(s);
18
+            }
19
+        }
20
+        return container.stream().toArray(String[]::new);
44 21
     }
45 22
 
46
-
47
-
48 23
     public static Boolean isPalindrome(String string) {
49
-        return null;
24
+        if (reverseString(string).equals(string)) {
25
+            return true;
26
+        } else {
27
+            return false;
28
+        }
50 29
     }
51 30
 
52 31
     public static String reverseString(String string) {
53
-        return null;
32
+        StringBuilder reverse = new StringBuilder();
33
+
34
+        for (int i = string.length() - 1; i >= 0; i--) {
35
+            reverse.append(string.charAt(i));
36
+        }
37
+        return reverse.toString();
54 38
     }
55 39
 }

+ 0
- 1
src/main/java/rocks/zipcode/io/quiz4/fundamentals/StringEvaluator.java 查看文件

@@ -2,7 +2,6 @@ package rocks.zipcode.io.quiz4.fundamentals;
2 2
 
3 3
 import java.util.SortedSet;
4 4
 import java.util.TreeSet;
5
-import java.util.stream.Collectors;
6 5
 
7 6
 /**
8 7
  * @author leon on 11/12/2018.