Skip to content

Commit ae7ad82

Browse files
committed
647
1 parent 442670d commit ae7ad82

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

647.CountSubstrings.cs

+27-21
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,42 @@
77
// A substring is a contiguous sequence of characters within the string.
88

99
public class Solution {
10-
public static void Main(string[] args)
11-
{
12-
Console.WriteLine ("Hello Mono World");
13-
count("abba");
14-
}
1510
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;
2316
}
2417

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;
2823
}
2924
}
3025

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;
3633
i++;
3734
j++;
3835
}
3936
}
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;
4147
}
4248
}

0 commit comments

Comments
 (0)