-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathpalindromic-substrings.java
More file actions
37 lines (35 loc) · 974 Bytes
/
palindromic-substrings.java
File metadata and controls
37 lines (35 loc) · 974 Bytes
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
class Solution {
public int countSubstrings(String s) {
int n = s.length();
int[][] dp = new int[n][n];
int ans = 0;
for (int i= 0; i<n; i++) {
for (int j = i; j<n; j++) {
if (isPal(s, i, j, dp)) {
ans ++;
}
}
}
return ans;
}
private boolean isPal (String s, int i, int j, int[][] dp) {
if (i >= j) {
return true;
}
else if (dp[i][j] != 0) {
return dp[i][j] == 1;
}
else {
boolean ans = false;
if (s.charAt(i) == s.charAt(j)) {
ans = isPal(s, i+1, j-1, dp);
}
else {
ans = false;
}
dp[i][j] = ans ? 1 : -1;
return ans;
}
}
}
// submission link: https://leetcode.com/problems/palindromic-substrings/submissions/620740483/