We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2fa579b commit 0d8c748Copy full SHA for 0d8c748
palindromic-substrings/ekgns33.java
@@ -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