-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCountPS.java
32 lines (29 loc) · 966 Bytes
/
CountPS.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
class Solution {
public int CountPS(String str, int n) {
// Given Size >= 2 if it's not then ans++ in gap == 0 also
boolean dp[][] = new boolean[n][n];
int ans = 0;
for(int gap=0; gap<n; gap++) {
for(int i=0, j=gap; j<n; i++, j++) {
if(gap == 0) {
dp[i][j] = true;
} else if (gap == 1) {
if(str.charAt(i) == str.charAt(j)) {
dp[i][j] = true;
ans++;
} else {
dp[i][j] = false;
}
} else {
if(str.charAt(i) == str.charAt(j) && dp[i+1][j-1]) {
dp[i][j] = true;
ans++;
} else {
dp[i][j] = false;
}
}
}
}
return ans;
}
}