-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromic_Substrings.cpp
More file actions
35 lines (34 loc) · 926 Bytes
/
Palindromic_Substrings.cpp
File metadata and controls
35 lines (34 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# number : 647
class Solution {
public:
int countSubstrings(string s) {
int result = 0;
int length = (int)s.size();
if (length == 0)
return result;
int **dp = new int*[length];
for (int i = 0; i < length; i++)
dp[i] = new int[length];
for (int i = 0; i < length; i++)
for (int j = 0; j <= i; j++)
{
dp[i][j] = 0;
if (i == j)
{
dp[i][j] = 1;
result += 1;
}
else if (i - j == 1 && s[i] == s[j])
{
dp[i][j] = 1;
result += 1;
}
else if (s[i] == s[j] && dp[i-1][j+1] == 1)
{
dp[i][j] = 1;
result += 1;
}
}
return result;
}
};