Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created September 6, 2017 13:18
Show Gist options
  • Save cixuuz/36331d63617bc8a1c1f54549cdaa4487 to your computer and use it in GitHub Desktop.
Save cixuuz/36331d63617bc8a1c1f54549cdaa4487 to your computer and use it in GitHub Desktop.
[647. Palindromic Substrings] #leetcode
class Solution {
// O(n^2) O(n^2)
public int countSubstrings(String s) {
int n = s.length();
int[][] dp = new int[n][n];
int res = 0;
for (int i = n - 1; i >= 0; i--) {
dp[i][i] = 1;
res++;
for (int j = i+1; j < n; j++) {
if (s.charAt(i) == s.charAt(j) && (i+1 == j || dp[i+1][j-1] == 1)) {
dp[i][j] = 1;
res++;
}
}
}
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment