Created
May 26, 2018 06:34
-
-
Save BiruLyu/62a68e59c7bce2f2a9f160704c8f7814 to your computer and use it in GitHub Desktop.
This file contains 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 int countSubstrings(String s) { | |
if (s == null || s.length() < 0) return 0; | |
int count = 0, len = s.length(); | |
boolean[][] isPalindrome = new boolean[len][len]; | |
for (int i = len - 1; i >= 0; i--) { | |
for (int j = i; j < len; j++) { | |
if (s.charAt(i) == s.charAt(j) && (j - i < 2 || isPalindrome[i + 1][j - 1])) { | |
isPalindrome[i][j] = true; | |
count++; | |
} | |
} | |
} | |
return count; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment