Skip to content

Instantly share code, notes, and snippets.

@BiruLyu
Created May 26, 2018 06:34
Show Gist options
  • Save BiruLyu/62a68e59c7bce2f2a9f160704c8f7814 to your computer and use it in GitHub Desktop.
Save BiruLyu/62a68e59c7bce2f2a9f160704c8f7814 to your computer and use it in GitHub Desktop.
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