Created
September 6, 2017 13:18
-
-
Save cixuuz/36331d63617bc8a1c1f54549cdaa4487 to your computer and use it in GitHub Desktop.
[647. Palindromic Substrings] #leetcode
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 { | |
// 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