Bonus Exercise 4 completed.
This commit is contained in:
@@ -76,11 +76,27 @@ public class BonusExercises {
|
|||||||
*/
|
*/
|
||||||
public List<String> findPalindromes(String string) {
|
public List<String> findPalindromes(String string) {
|
||||||
List<String> list = new ArrayList<>();
|
List<String> list = new ArrayList<>();
|
||||||
// todo
|
|
||||||
|
String regex = "[^a-zA-Z]+";
|
||||||
|
|
||||||
|
//Splitting the string by anything except words to check each one
|
||||||
|
String[] words = string.split(regex);
|
||||||
|
|
||||||
|
for (String word : words) {
|
||||||
|
if (word.length() >= 3) {
|
||||||
|
//Case-insensitive
|
||||||
|
String lower = word.toLowerCase();
|
||||||
|
String reversed = new StringBuilder(lower).reverse().toString();
|
||||||
|
|
||||||
|
//Checking if it's palindrome or not
|
||||||
|
if (lower.equals(reversed)) {
|
||||||
|
list.add(word);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {}
|
||||||
// you can test your code here
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user