Implement intPartitions method
This commit is contained in:
@@ -65,11 +65,41 @@ public class BonusExercises {
|
||||
note: your implementation should be case-insensitive, e.g. Aba -> is palindrome
|
||||
*/
|
||||
public List<String> findPalindromes(String string) {
|
||||
List<String> list = new ArrayList<>();
|
||||
// todo
|
||||
return list;
|
||||
List<String> result = new ArrayList<>();
|
||||
String[] words = string.split("\\s+");
|
||||
for (int i = 0; i < words.length; i++) {
|
||||
|
||||
String word = words[i];
|
||||
word = word.replaceAll("[^a-zA-Z]", "");
|
||||
|
||||
if (word.length() < 3) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String lower = word.toLowerCase();
|
||||
int left = 0;
|
||||
int right = lower.length() - 1;
|
||||
|
||||
boolean isPalindrome = true;
|
||||
while (left < right) {
|
||||
|
||||
if (lower.charAt(left) != lower.charAt(right)) {
|
||||
isPalindrome = false;
|
||||
break;
|
||||
}
|
||||
left++;
|
||||
right--;
|
||||
}
|
||||
|
||||
if (isPalindrome) {
|
||||
result.add(word);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
// you can test your code here
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user