File tree Expand file tree Collapse file tree 1 file changed +16
-10
lines changed Expand file tree Collapse file tree 1 file changed +16
-10
lines changed Original file line number Diff line number Diff line change @@ -4,19 +4,25 @@ public int countSubstrings(String s) {
4
4
return 0 ;
5
5
}
6
6
7
- int result = 0 , n = s .length ();
8
- boolean [][] dp = new boolean [n ][n ];
7
+ int result = 0 ;
9
8
10
- for (int i = n - 1 ; i >= 0 ; i --) {
11
- for (int j = i ; j < n ; j ++) {
12
- dp [i ][j ] = s .charAt (i ) == s .charAt (j ) && (j - i < 2 || dp [i + 1 ][j - 1 ]);
13
-
14
- if (dp [i ][j ]) {
15
- ++result ;
16
- }
17
- }
9
+ for (int i = 0 ; i < s .length (); i ++) {
10
+ result += expand (s , i , i );
11
+ result += expand (s , i , i + 1 );
18
12
}
19
13
20
14
return result ;
21
15
}
16
+
17
+ private int expand (String str , int s , int e ) {
18
+ int count = 0 ;
19
+
20
+ while (s >= 0 && e < str .length () && str .charAt (s ) == str .charAt (e )) {
21
+ --s ;
22
+ ++e ;
23
+ ++count ;
24
+ }
25
+
26
+ return count ;
27
+ }
22
28
}
You can’t perform that action at this time.
0 commit comments