File tree 1 file changed +27
-21
lines changed
1 file changed +27
-21
lines changed Original file line number Diff line number Diff line change 7
7
// A substring is a contiguous sequence of characters within the string.
8
8
9
9
public class Solution {
10
- public static void Main ( string [ ] args )
11
- {
12
- Console . WriteLine ( "Hello Mono World" ) ;
13
- count ( "abba" ) ;
14
- }
15
10
public int CountSubstrings ( string s ) {
16
-
17
- }
18
- public static int count ( string s ) {
19
- bool [ , ] dp = new bool [ s . Length , s . Length ] ;
20
-
21
- for ( int i = 0 ; i < s . Length ; i ++ ) {
22
- dp [ i ] [ i ] = true ;
11
+ bool [ , ] dp = new bool [ s . Length , s . Length ] ;
12
+ Console . WriteLine ( dp . Length ) ;
13
+ for ( int i = 0 ; i < s . Length ; i ++ )
14
+ {
15
+ dp [ i , i ] = true ;
23
16
}
24
17
25
- for ( int i = 1 ; i < s . Length ; i ++ ) {
26
- if ( s [ i ] == s [ i - 1 ] ) {
27
- dp [ i ] [ i - 1 ] = true ;
18
+ for ( int i = 1 ; i < s . Length ; i ++ )
19
+ {
20
+ if ( s [ i ] == s [ i - 1 ] )
21
+ {
22
+ dp [ i , i - 1 ] = true ;
28
23
}
29
24
}
30
25
31
- for ( int d = 2 ; d < s . Length ; d ++ ) {
32
- int i = 0 ; int j = d ;
33
- while ( j < s . Length ) {
34
- if ( s [ i ] == s [ j ] && dp [ i + 1 ] [ j - 1 ] )
35
- dp [ i ] [ j ] = true ;
26
+ for ( int d = 2 ; d < s . Length ; d ++ )
27
+ {
28
+ int i = 0 ; int j = d ;
29
+ while ( j < s . Length )
30
+ {
31
+ if ( s [ i ] == s [ j ] && dp [ j - 1 , i + 1 ] )
32
+ dp [ j , i ] = true ;
36
33
i ++ ;
37
34
j ++ ;
38
35
}
39
36
}
40
-
37
+ int count = 0 ;
38
+ for ( int i = 0 ; i < dp . GetLength ( 0 ) ; i ++ )
39
+ {
40
+ for ( int j = i ; j < dp . GetLength ( 1 ) ; j ++ )
41
+ {
42
+ if ( dp [ j , i ] )
43
+ count ++ ;
44
+ }
45
+ }
46
+ return count ;
41
47
}
42
48
}
You can’t perform that action at this time.
0 commit comments