Skip to content

Commit 78a5705

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 647_Palindromic_Substrings.java
1 parent f8ba038 commit 78a5705

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int countSubstrings(String s) {
3+
if (s == null || s.length() == 0) {
4+
return 0;
5+
}
6+
7+
int result = 0, n = s.length();
8+
boolean[][] dp = new boolean[n][n];
9+
10+
for (int i = n - 1; i >= 0; i--) {
11+
for (int j = i; j < n; j++) {
12+
dp[i][j] = s.charAt(i) == s.charAt(j) && (j - i < 2 || dp[i + 1][j - 1]);
13+
14+
if (dp[i][j]) {
15+
++result;
16+
}
17+
}
18+
}
19+
20+
return result;
21+
}
22+
}

0 commit comments

Comments
 (0)