Skip to content

Commit 0d8c748

Browse files
committed
feat : palindromic-substrings
1 parent 2fa579b commit 0d8c748

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

palindromic-substrings/ekgns33.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
input : string s
3+
output: the number of palindromic substrings of given string
4+
tc : O(n^2) sc : o(n^2) when n is the length of string s
5+
6+
optimize?
7+
maybe nope.. atleast n^2 to select i and j
8+
9+
*/
10+
class Solution {
11+
public int countSubstrings(String s) {
12+
int n = s.length();
13+
int cnt = 0;
14+
boolean[][] dp = new boolean[n][n];
15+
for(int i = n-1; i >= 0; i--) {
16+
for(int j = i; j < n; j++) {
17+
dp[i][j] = s.charAt(i) == s.charAt(j) && (j-i +1 < 3 || dp[i+1][j-1]);
18+
if(dp[i][j]) cnt++;
19+
}
20+
}
21+
return cnt;
22+
}
23+
}
24+
// Compare this snippet from valid-palindrome-ii/ekgns33.java:

0 commit comments

Comments
 (0)