Skip to content

Instantly share code, notes, and snippets.

@BrentonPoke
Created July 29, 2019 08:49
Show Gist options
  • Save BrentonPoke/464057ead2f3d5315b7d8d37fd494e97 to your computer and use it in GitHub Desktop.
Save BrentonPoke/464057ead2f3d5315b7d8d37fd494e97 to your computer and use it in GitHub Desktop.
class Solution {
public static int countPalindromes(String s) {
return findAllPalindromesUsingCenter(s);
}
public static int findAllPalindromesUsingCenter(String input) {
List<String> palindromes = new ArrayList<>();
for (int i = 0; i < input.length(); i++) {
palindromes.addAll(findPalindromes(input, i, i + 1));
palindromes.addAll(findPalindromes(input, i, i));
}
System.out.print(palindromes.toString());
return palindromes.size();
}
private static List<String> findPalindromes(String input, int low, int high) {
List<String> result = new ArrayList<>();
while (low >= 0 && high < input.length() && input.charAt(low) == input.charAt(high)) {
result.add(input.substring(low, high + 1));
low--;
high++;
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment