Implement findPalindromes method
This commit is contained in:
@@ -84,10 +84,32 @@ 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[] words = string.split("\\s+|,");
|
||||||
|
for(String word : words){
|
||||||
|
if(word.length() >= 3 && isPalindrome(word)){
|
||||||
|
list.add(word);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isPalindrome(String string){
|
||||||
|
boolean result = true;
|
||||||
|
int counter = 0;
|
||||||
|
while(counter + 1 <= string.length() / 2){
|
||||||
|
char ch1 = string.charAt(counter);
|
||||||
|
char ch2 = string.charAt(string.length() - 1 - counter);
|
||||||
|
if(Character.toLowerCase(ch1) != Character.toLowerCase(ch2)){
|
||||||
|
result = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// you can test your code here
|
// you can test your code here
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user