Implement intPartitions method

This commit is contained in:
Arefe Talebi
2026-04-21 01:03:04 +03:30
parent e73ac6115f
commit 1c41ea38a2
2 changed files with 124 additions and 21 deletions
+33 -3
View File
@@ -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
}