File tree Expand file tree Collapse file tree 1 file changed +20
-29
lines changed Expand file tree Collapse file tree 1 file changed +20
-29
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
-
3
2
public int countSubstrings (String s ) {
4
- if (s .length () < 2 ) {
5
- return s .length ();
6
- }
7
- int result = 0 ;
3
+ int res = 0 ;
4
+
8
5
for (int i = 0 ; i < s .length (); i ++) {
9
- // Odd Length
10
- int left = i , right = i ;
11
- while (
12
- left >= 0 &&
13
- right < s .length () &&
14
- s .charAt (left ) == s .charAt (right )
15
- ) {
16
- result ++;
17
- left --;
18
- right ++;
19
- }
20
- // Even Length
21
- left = i ;
22
- right = i + 1 ;
23
- while (
24
- left >= 0 &&
25
- right < s .length () &&
26
- s .charAt (left ) == s .charAt (right )
27
- ) {
28
- result ++;
29
- left --;
30
- right ++;
31
- }
6
+ res += countSubstrings (s , i , i );
7
+ res += countSubstrings (s , i , i + 1 );
8
+ }
9
+
10
+ return res ;
11
+ }
12
+
13
+ public int countSubstrings (String s ,
14
+ int start , int end ) {
15
+ int res = 0 ;
16
+
17
+ while (start >= 0 && end < s .length ()
18
+ && s .charAt (start ) == s .charAt (end )) {
19
+ ++res ;
20
+ --start ;
21
+ ++end ;
32
22
}
33
- return result ;
23
+
24
+ return res ;
34
25
}
35
26
}
You can’t perform that action at this time.
0 commit comments