-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetCodeQ647.java
More file actions
37 lines (34 loc) · 1.11 KB
/
leetCodeQ647.java
File metadata and controls
37 lines (34 loc) · 1.11 KB
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
package DynamicProgramming;
public class leetCodeQ647 {
public static void main(String[] args) {
String s = "aaa" ;
int n = s.length();
int count = 0;
int[][] dp = new int[n][n];
for (int k = 0; k < n; k++) { // Loop kitni baarr chl raha hai
int i = 0, j = k;
while (j < n) {
if (i == j) { // If Length of Substring is 1 .
dp[i][j] = 1;
count++;
}
else if (j == i + 1) { // if Length of substring is 2 .
if (s.charAt(i) == s.charAt(j)) {
dp[i][j] = 1;
count++;
}
} else { // if Length of substring is more than 2 .
if (s.charAt(i) == s.charAt(j)) {
if (dp[i + 1][j - 1] == 1) {
dp[i][j] = 1;
count++;
}
}
}
i++;
j++;
}
}
System.out.println(count);
}
}