Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static int count;
- public int countSubstrings(String str) {
- count = 0;
- for (int i = 0; i < str.length(); i++) {
- /**
- * Expand for the odd length palindrome string
- */
- expandPalindrome(str, i, i);
- /**
- * Expand for the even length palindrome string
- */
- expandPalindrome(str, i, i + 1);
- }
- return count;
- }
- private void expandPalindrome(String str, int i, int j) {
- while (i >= 0 && j < str.length()) {
- if (str.charAt(i) == str.charAt(j)) {
- count++;
- } else {
- break;
- }
- i--;
- j++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement