Created
July 29, 2019 08:49
-
-
Save BrentonPoke/464057ead2f3d5315b7d8d37fd494e97 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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