-
Notifications
You must be signed in to change notification settings - Fork 2
/
647.palindromic-substrings.java
47 lines (42 loc) · 1.12 KB
/
647.palindromic-substrings.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* @lc app=leetcode id=647 lang=java
*
* [647] Palindromic Substrings
*/
// @lc code=start
class Solution {
public int countSubstrings(String s) {
int n = s.length();
if (n == 0) {
return 0;
}
boolean[][] dp = new boolean[n][n];
//Base case1: only 1 character which is palindrome
for (int i = 0; i < n; i++) {
dp[i][i] = true;
}
//Base case2 with dp function
for (int length = 2; length <= n; length++) {
for (int i = 0; i <= n - length; i++) {
int j = i + length - 1;
if (length == 2 && s.charAt(i) == s.charAt(j)) {
dp[i][j] = true;
}
if (dp[i + 1][j - 1] && s.charAt(i) == s.charAt(j)) {
dp[i][j] = true;
}
}
}
//count
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dp[i][j]) {
count++;
}
}
}
return count;
}
}
// @lc code=end